When I run dotnet ef migrations add Initial_Identity
, I got this error:
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.
How can I solve it?
Here is my code:
Startup class:
public void ConfigureServices(IServiceCollection services)
{
// some codes
services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}
Program 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();
}
TemporaryDbContextFactory class:
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");
}
Related to Identity:
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> { }
UPDATE 1: The question has been updated!
According to Tao's answer, I corrected my code. Now the yellow error is solved but now there appeared a red error!
How can I solve this error?
Value cannot be null. Parameter name: connectionString
Contain of appsettings.json file:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source='';initial catalog=Jahan-Beta;User ID=sa;password=''; Persist Security Info=True; Encrypt=False;TrustServerCertificate=False; MultipleActiveResultSets=True"
}
}
I also copied appsettings.json in \bin\Debug\netcoreapp2.0
directory.
Since you have inherited ApplicationDbContext
from IdentityDbContext<User, CustomRole, int, CustomUserClaim, CustomUserRole, CustomUserLogin, CustomRoleClaim, CustomUserToken>
, you should use CustomRole
instead of IdentityRole
while AddIdentity
.
services.AddIdentity<User, CustomRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();