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

问题描述

我想mydbcontext整合IdentityContext,但我采取这一误差

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

Ivdb.Dal.Concrete.EF codeFirst.IdentityUserLogin:的EntityType'IdentityUserLogin没有定义键。定义此的EntityType的关键。
Ivdb.Dal.Concrete.EF codeFirst.IdentityUserRole:的EntityType'IdentityUserRole没有定义键。定义此的EntityType的关键。
IdentityUserLogins:的EntityType:EntitySet的'IdentityUserLogins'是基于类型'IdentityUserLogin'不具有自定义键。
IdentityUserRoles:的EntityType:EntitySet的'IdentityUserRoles'是基于类型'IdentityUserRole'是没有定义的键

我怎样才能解决呢?

code:

 公共部分类ivdbDb156978Context:IdentityDbContext< ApplicationUser>
{
    静态ivdbDb156978Context()
    {
        Database.SetInitializer< ivdbDb156978Context>(NULL);
    }    公共ivdbDb156978Context()
        :基地(NAME = ivdbContext)
    {
    }
    公共DbSet<汽车>汽车{搞定;组; }

应用程序用户

 公共类ApplicationUser:IdentityUser
{}


解决方案

您code简化版,证明这一点,但是从错误中你得到我假设你要重写OnModelCreating.This就是 IdentityDbContext< ApplicationUser> 配置实体框架映射。这意味着,如果你想覆盖OnModelCreating你需要或者调用基或者必须自己做的映射。

因此​​,无论这样的:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
    base.OnModelCreating(模型构建器);    //你这里的东西
}

或者你做的映射:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
    modelBuilder.Entity< IdentityUserLogin>()HasKey<串GT(L => l.UserId);
    modelBuilder.Entity< IdentityRole>()HasKey<串GT(R = GT; r.Id);
    modelBuilder.Entity< IdentityUserRole方式>()HasKey(R = gt;新建{r.RoleId,r.UserId});
}

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' 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 ?

Code:

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

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


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

Application User

    public class ApplicationUser : IdentityUser
{

}
解决方案

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.

So either this:

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

    // your stuff here
}

Or you do the mapping:

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的身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:09