本文介绍了Asp.net 身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 IdentityContext 与 mydbcontext 集成,但我遇到了这个错误

I want integrate IdentityContext with mydbcontext but i am taking this error

在模型生成过程中检测到一个或多个验证错误:

One or more validation errors were detected during model generation:

Ivdb.Dal.Concrete.EFCodeFirst.IdentityUserLogin:: EntityType 'IdentityUserLogin' 没有定义键.定义此 EntityType 的键.Ivdb.Dal.Concrete.EFCodeFirst.IdentityUserRole:: EntityType 'IdentityUserRole' 没有定义键.定义此 EntityType 的键.IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' 基于未定义键的类型 'IdentityUserLogin'.IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' 基于未定义键的类型 'IdentityUserRole'.

Ivdb.Dal.Concrete.EFCodeFirst.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.Ivdb.Dal.Concrete.EFCodeFirst.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

我该如何解决这个问题?

How can i solve this ?

代码:

 public partial class ivdbDb156978Context : IdentityDbContext<ApplicationUser>
{
    static ivdbDb156978Context()
    {
        Database.SetInitializer<ivdbDb156978Context>(null);
    }

    public ivdbDb156978Context()
        : base("Name=ivdbContext")
    {
    }


    public DbSet<Car> Cars { get; set; }

应用用户

    public class ApplicationUser : IdentityUser
{

}

推荐答案

你的代码没有显示这一点,但从你得到的错误来看,我假设你正在覆盖 OnModelCreating.这就是 IdentityDbContext<ApplicationUser> 配置实体框架映射.这意味着,如果您想覆盖 OnModelCreating,您需要调用基础或您必须自己进行映射.

Your code does't show this, but from the errors you are getting I assume that you are overriding OnModelCreating.This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

所以要么这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // your stuff here
}

或者你做映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

这篇关于Asp.net 身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 08:17