在 class 'Program' 上调用方法 'BuildWebHost' 时出错
An error occurred while calling method 'BuildWebHost' on class 'Program'
当我 运行 dotnet ef migrations add Initial_Identity
时,我得到了这个错误:
An error occurred while calling method 'BuildWebHost' on class
'Program'. Continuing without the application service provider. Error:
GenericArguments1, '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'. An operation was scaffolded
that may result in the loss of data. Please review the migration for
accuracy.
我该如何解决?
这是我的代码:
启动class:
public void ConfigureServices(IServiceCollection services)
{
// some codes
services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}
程序class:
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();
}
TemporaryDbContextFactoryclass:
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 class:
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> { }
更新一:问题已更新!
根据,我更正了我的代码。现在黄色的错误已经解决了,但是现在出现了红色的错误!
如何解决这个错误?
Value cannot be null. Parameter name: 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"
}
}
我也在\bin\Debug\netcoreapp2.0
目录下复制了appsettings.json
由于您从 IdentityDbContext<User, CustomRole, int, CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim, CustomUserToken>
继承了 ApplicationDbContext
,因此在 AddIdentity
时,您应该使用 CustomRole
而不是 IdentityRole
。
services.AddIdentity<User, CustomRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
当我 运行 dotnet ef migrations add Initial_Identity
时,我得到了这个错误:
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: GenericArguments1, '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'. An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
我该如何解决?
这是我的代码:
启动class:
public void ConfigureServices(IServiceCollection services)
{
// some codes
services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}
程序class:
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();
}
TemporaryDbContextFactoryclass:
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 class:
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> { }
更新一:问题已更新!
根据
如何解决这个错误?
Value cannot be null. Parameter name: 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"
}
}
我也在\bin\Debug\netcoreapp2.0
目录下复制了appsettings.json
由于您从 IdentityDbContext<User, CustomRole, int, CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim, CustomUserToken>
继承了 ApplicationDbContext
,因此在 AddIdentity
时,您应该使用 CustomRole
而不是 IdentityRole
。
services.AddIdentity<User, CustomRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();