结合我ApplicationsDbContext与IdentityDbContext使用MVC 5无按键定义 [英] Combine my ApplicationsDbContext with IdentityDbContext no key defined using MVC 5

查看:1040
本文介绍了结合我ApplicationsDbContext与IdentityDbContext使用MVC 5无按键定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我结合我在MVC 5.新IdentityDbContext现有的DbContext我设法在两种情况结合起来,但是当我跑我的应用程序,并正在创建模型我是跟以下错误消息psented $ P $:

I was combining my existing DBContext with the new IdentityDbContext in MVC 5. I managed to combine the two contexts but when I ran my application and the model was being created I was presented with the following error message:

Context.IdentityUserLogin:的EntityType'IdentityUserLogin没有定义键。定义此的EntityType的关键。
Context.IdentityUserRole:的EntityType'IdentityUserRole没有定义键。定义此的EntityType关键。

Context.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

推荐答案

我的工作如何做阅读后位固定。

I worked out how to fix it after doing a bit of reading.

创建以下两种配置类(保持简洁你OnModelCreating方法作为数据库的增长和表之间的关系增加)

Create the two following configuration classes (keeps it clean in your OnModelCreating method as your database grows and relationships between tables increase)

public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
{

    public IdentityUserLoginConfiguration()
    {
        HasKey(iul => iul.UserId);
    }

}

public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
{

    public IdentityUserRoleConfiguration()
    {
        HasKey(iur => iur.RoleId);
    }

}

在你的应用程序中的OnModelCreating方法的DbContext添加上述到模型中的两种配置:

In the OnModelCreating method within your Applications DbContext add the two configurations outlined above to the model:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
        modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());

    }

在创建模型时,这个现在应该摆脱的错误方法。这为我做的。

This should now get rid of the error methods when your model is being created. It did for me.

这篇关于结合我ApplicationsDbContext与IdentityDbContext使用MVC 5无按键定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆