简体   繁体   English

迁移问题 AspNetCore API

[英]Migration Issue AspNetCore API

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.我面临图像中显示的以下相关问题。

在此处输入图像描述

Here is my dbContext class这是我的 dbContext class

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

    }

Here is are License User and LicenseUserRole classes这是 License User 和 LicenseUserRole 类

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.我正在使用 EF Core 版本 5.0.9。 It always says installation of both EF6 and EFCore though I have installed only Core.尽管我只安装了 Core,但它总是说安装了 EF6 和 EFCore。

An error occurred while accessing the Microsoft.Extensions.Hosting services.访问 Microsoft.Extensions.Hosting 服务时出错。 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'.错误:GenericArguments[1],'Microsoft.AspNetCore.Identity.IdentityRole',在'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9 [TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]'违反了“TRole”类型的约束。

For this issue, I can reproduce it when I register wrong IdentityRole .对于这个问题,我可以在注册错误的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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM