在类“Program”上调用方法“BuildWebHost”时出错

Jah*_*han 3 asp.net entity-framework-core asp.net-core-2.0 ef-core-2.0 entity-framework-migrations

当我运行时 dotnet ef migrations add Initial_Identity,出现此错误:

在类“Program”上调用方法“BuildWebHost”时出错。在没有应用程序服务提供商的情况下继续。错误:GenericArguments 1 , 'Microsoft.AspNetCore.Identity.IdentityRole', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]'s 违反了'TRole' 类型的约束。脚手架操作可能会导致数据丢失。请检查迁移的准确性。

我该如何解决?

这是我的代码:

创业班

public void ConfigureServices(IServiceCollection services)
{
    // some codes
    services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}
Run Code Online (Sandbox Code Playgroud)

程序类

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseDefaultServiceProvider(options => options.ValidateScopes = false)
            .Build();
}
Run Code Online (Sandbox Code Playgroud)

TemporaryDbContextFactory 类

public class TemporaryDbContextFactory : 
IDesignTimeDbContextFactory<ApplicationDbContext>
{
    //////// 
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json")
            .Build();

        builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
        return new ApplicationDbContext(builder.Options);
    }
}
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext 类

public class ApplicationDbContext : IdentityDbContext<User, CustomRole, int,   CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim,   CustomUserToken>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }

// some codes
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CustomUserLogin>().HasKey(a => new { a.UserId });
        modelBuilder.Entity<CustomUserRole>().HasKey(a => new { a.RoleId, a.UserId });
        modelBuilder.Entity<CustomUserToken>().HasKey(a => new { a.UserId});
    }

 // some codes

        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<CustomRole>().ToTable("Role");
        modelBuilder.Entity<CustomRoleClaim>().ToTable("RoleClaim");
        modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<CustomUserRole>().ToTable("UserRole");
        modelBuilder.Entity<CustomUserToken>().ToTable("UserToken");

}
Run Code Online (Sandbox Code Playgroud)

与身份相关:

public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserToken : IdentityUserToken<int> { }
public class CustomRole : IdentityRole<int> { }
public class CustomRoleClaim : IdentityRoleClaim<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class User : IdentityUser<int> { }
Run Code Online (Sandbox Code Playgroud)

错误

更新1:问题已更新!

根据陶的回答,我更正了我的代码。现在黄色错误解决了,但现在出现了一个红色错误!

我该如何解决这个错误?

值不能为空。参数名称:connectionString

值不能为空

包含 appsettings.json 文件:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source='';initial catalog=Jahan-Beta;User ID=sa;password=''; Persist Security Info=True; Encrypt=False;TrustServerCertificate=False; MultipleActiveResultSets=True"
  }
}
Run Code Online (Sandbox Code Playgroud)

我还在\bin\Debug\netcoreapp2.0目录中复制了 appsettings.json 。

Tao*_*hou 5

由于您继承了ApplicationDbContextfrom IdentityDbContext<User, CustomRole, int, CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim, CustomUserToken>,您应该使用CustomRole而不是IdentityRolewhile AddIdentity

services.AddIdentity<User, CustomRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)