Home > Blockchain >  Migration Issue AspNetCore API
Migration Issue AspNetCore API

Time:12-24

I am working on a project in which I am using the code-first approach to add migration of my existing entities. I am facing the following related issue shown in the image.

enter image description here

Here is my dbContext class

 public class LicenseDbContext: IdentityDbContext<LicenseUser, LicenseUserRole, long>
    {
        public LicenseDbContext(
           DbContextOptions<LicenseDbContext> options
           ) : base(options)
        {
        }

    }

Here is are License User and LicenseUserRole classes

public class LicenseUser : IdentityUser<long>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ApplicationRoleEnum UserRole { get; set; }
    }
    
 public class LicenseUserRole : IdentityRole<long>
    {
        public LicenseUserRole() : base()
        {
        }

        public LicenseUserRole(string roleName) : base(roleName)
        {

        }
    }

I am using EF Core version 5.0.9. It always says installation of both EF6 and EFCore though I have installed only Core.

CodePudding user response:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: GenericArguments[1], 'Microsoft.AspNetCore.Identity.IdentityRole', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type 'TRole'.

For this issue, I can reproduce it when I register wrong IdentityRole.

Be sure register your service like below:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<LicenseDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<LicenseUser, LicenseUserRole>(options => options.SignIn.RequireConfirmedAccount = false)
        .AddEntityFrameworkStores<LicenseDbContext>();
    //other services...
}
  • Related