可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
The following solution works in .net core 1.1, but after upgrading from 1.1 to 2.0, I received the following error:
InvalidOperationException: Cannot create a DbSet for 'Role' because this type is not included in the model for the context.
When the user attempts to log in and the following statement is executed:
var result = await _signInManager.PasswordSignInAsync(model.Email,
model.Password, model.RememberMe, lockoutOnFailure: false);
What is wrong?
User.cs
public partial class User : IdentityUser<Guid>
{
public string Name { get; set; }
}
IdentityEntities.cs
public partial class UserLogin : IdentityUserLogin<Guid>
{
}
public partial class UserRole : IdentityUserRole<Guid>
{
}
public partial class UserClaim : IdentityUserClaim<Guid>
{
}
public partial class Role : IdentityRole<Guid>
{
public Role() : base()
{
}
public Role(string roleName)
{
Name = roleName;
}
}
public partial class RoleClaim : IdentityRoleClaim<Guid>
{
}
public partial class UserToken : IdentityUserToken<Guid>
{
}
ConfigureServices
services.AddIdentity<User, Role>
回答1:
Added this and it worked:
builder.Entity<IdentityUserRole<Guid>>().HasKey(p => new { p.UserId, p.RoleId });
回答2:
Check that your AppDbContext
is NOT inherited from DbContext
but instead it should be inherited from IdentityDbContext<ApplicationUser>
回答3:
The most common reasons for
Cannot create a DbSet for 'THE-MODEL' because this type is not
included in the model for the context
are as follows
- The model name does not match with the table name in database
- EntityFramework cannot figure out required meta by convention and you have
not overriden it.
in your case Role inherits IdentityRoleClaim and that was not configured and default convention required "Id" as key but i suppose it did not have that property so it had to be configured. It would also have worked if you created property in Role like Id => new{UserId,RoleId} which would by convention present Id as the key property to entity framework.
回答4:
If your DbContext doesn't inherit IdentityUserContext - don't use AddEntityFrameworkStores in the ConfigureServices method. Substitute it with AddUserStore and AddRoleStore as follows:
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddIdentity<MyUserType, MyRoleType>(...)
.AddUserStore<UserStore<MyUserType, MyRoleType, MyDbContextType, MyKeyType, MyUserClaimType, MyUserRoleType, MyUserLoginType, MyUserTokenType, MyRoleClaimType>>()
.AddRoleStore<RoleStore<MyRoleType, MyDbContextType, MyKeyType, MyUserRoleType, MyRoleClaimType>>();
...
}
If you check the implementation of the AddEntityFrameworkStores method you'll see that it adds stores via searching for generic types in the DbContext, assuming that it inherits IdentityUserContext. Regardless, you'll be stuck with the base Identity* types for claims ... which will generate this exception.
回答5:
You can fix the issue by replacing this
public class ApplicationDbContext : IdentityDbContext<AppUser>
with this
public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, string>
notice this string type in the last argument.
回答6:
I fixed this by adding:
public DbSet<ApplicationRole> ApplicationRoles { get; set; }
to my DbContext
.
回答7:
In my case this was happening because I was manually adding entities, and forgot to add the next one under DbContext.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
new CryptoCoinsMap(modelBuilder.Entity<CoinMaster>());
new CoinQuotesDailyMap(modelBuilder.Entity<CoinQuotesDaily>());
}
回答8:
I had the similar problem, this was from the bad configuration for IUserStore in my case. I'm using Autofac for dependency injection and this configuration solved my problem:
var dbContextParameter = new ResolvedParameter((pi, ctx) => pi.ParameterType == typeof(IdentityDbContext),
(pi, ctx) => ctx.Resolve<DatabaseContext>());
builder.RegisterType<UserStore<User, Role, DatabaseContext, Guid,UserClaim,UsersInRole,UserLogin,UserToken,RoleClaim>>()
.As<IUserStore<User>>().WithParameter(dbContextParameter).InstancePerLifetimeScope();
builder.RegisterType<CustomRoleStore>()
//RegisterType<UserStore<User, Role, DatabaseContext, Guid,UserClaim,UsersInRole,UserLogin,UserToken,RoleClaim>>()
.As<IRoleStore<Role>>().WithParameter(dbContextParameter).InstancePerLifetimeScope();
My databaseContext drives from IdentityDbContext
public class DatabaseContext : IdentityDbContext<User, Role, Guid, UserClaim
, UsersInRole, UserLogin, RoleClaim, UserToken
>, IDatabaseContext
and all I had to do was to Create a Userstore and give it to my DI to inject into signinmanager class
回答9:
Add your models in the DBContext using model builder.
Write your DB Context class as below.
public partial class CDBContext : DbContext
{
public CDBContext (string ConnectionString) : base(new DbContextOptionsBuilder().UseSqlServer(ConnectionString).Options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Query<MediaData>();
}
回答10:
I had this problem. To fixed it, I did some changes in my Entity Config Class.
public class AspNetUserClaimsConfig : IEntityTypeConfiguration<IdentityUserClaim<string>>
{
public void Configure(EntityTypeBuilder<IdentityUserClaim<string>> builder)
{
builder.ToTable("AspNetUserClaims", "Identity");
// Primary key
builder.HasKey(uc => uc.Id);
}
}
In my context I did this:
public class IdentityContext : DbContext
{
public IdentityContext(DbContextOptions<IdentityContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ApplicationUsersConfig());
base.OnModelCreating(modelBuilder);
}
}