在类'Program'上调用方法'BuildWebHost'时发生错误

时间:2018-03-13 02:32:39

标签: asp.net entity-framework-core asp.net-core-2.0 ef-migrations ef-core-2.0

当我运行dotnet ef migrations add Initial_Identity时,我收到了此错误:

  

在类上调用方法'BuildWebHost'时发生错误   '程序'。继续没有应用程序服务提供商。错误:   GenericArguments 1,'Microsoft.AspNetCore.Identity.IdentityRole',on   'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9 [TUSER,TRole,TContext,TKEY的,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]'   违反了“TRole”类型的约束。一项手术是脚手架   这可能会导致数据丢失。请查看迁移   精度。

我该如何解决?

这是我的代码:

启动课程

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

计划类

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();
}

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);
    }
}

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");

}

与身份相关:

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> { }

THE ERROR

更新1:问题已更新!

根据Tao's answer,我更正了我的代码。现在黄色错误已解决,但现在出现红色错误!

如何解决此错误?

  

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

Value cannot be null

包含appsettings.json文件:

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

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

1 个答案:

答案 0 :(得分:1)

由于您已从ApplicationDbContext继承IdentityDbContext<User, CustomRole, int, CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim, CustomUserToken>,因此CustomRole时应使用IdentityRole代替AddIdentity

services.AddIdentity<User, CustomRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();