'ASP.NET Core Web API - System.AggregateException: 'Unable to cast object of type 'System.Int64' to type 'System.String''
In ASP.NET Core-6 Web API, I have this code in Program.cs:
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
if (context.Database.IsSqlServer())
{
await context.Database.MigrateAsync();
}
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
ApplicationDbContextSeed.SeedData(userManager, roleManager);
}
catch (Exception ex)
{
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating or seeding the database.");
throw;
}
}
await host.RunAsync();
}
ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
ApplicationDbContextSeed
public static class ApplicationDbContextSeed
{
public static void SeedData(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
SeedRoles(roleManager);
SeedUsers(userManager);
}
public static void SeedRoles(RoleManager<IdentityRole> roleManager)
{
if (!roleManager.RoleExistsAsync("Admin").Result)
{
IdentityRole role = new IdentityRole();
role.Name = "Admin";
IdentityResult roleResult = roleManager.
CreateAsync(role).Result;
}
}
public static void SeedUsers(UserManager<ApplicationUser> userManager)
{
if (userManager.FindByNameAsync("admin").Result == null)
{
ApplicationUser user = new ApplicationUser()
{
UserName = "admin",
Email = "[email protected]",
FirstName = "Firstname1",
LastName = "Lastname1",
};
IdentityResult result = userManager.CreateAsync
(user, "Admin12345").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "Admin").Wait();
}
}
if (userManager.FindByNameAsync("admin2").Result == null)
{
ApplicationUser user = new ApplicationUser()
{
UserName = "admin2",
Email = "[email protected]",
FirstName = "Firsname2",
LastName = "Lastname2",
};
IdentityResult result = userManager.CreateAsync
(user, "Admin12345").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "Admin").Wait();
}
}
}
}
When I run the application, I got this error:
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (Unable to cast object of type 'System.Int64' to type 'System.String'.)
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at Core.Persistence.ApplicationDbContextSeed.SeedRoles(RoleManager`1 roleManager) in C:\MyApp\WebApi\Core\Persistence\ApplicationDbContextSeed.cs:line 20
at Core.Persistence.ApplicationDbContextSeed.SeedData(UserManager`1 userManager, RoleManager`1 roleManager) in C:\MyApp\WebApi\Core\Persistence\ApplicationDbContextSeed.cs:line 15
at WebApi.Program.<Main>d__0.MoveNext() in C:\MyApp\WebApi\Program.cs:line 46
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.
This is line 20:
if (!roleManager.RoleExistsAsync("Admin").Result)
This is line 15:
SeedRoles(roleManager);
The full error as displayed when I run the application is shown above. I got this while debugging. So I updated the initial one I have with this full error detail.
throw is highlighted.
How do I resolve this?
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
