"定义此的EntityType关键&QUOT。当包括类型ApplicationUser属性 [英] "Define the key for this EntityType." when including attribute of type ApplicationUser

查看:181
本文介绍了"定义此的EntityType关键&QUOT。当包括类型ApplicationUser属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写C#/ ASP.NET MVC5 Web应用程序,而我打下面的错误,当我尝试做一个新的迁移

  ToTheMoon.DAL.IdentityUserLogin:的EntityType'IdentityUserLogin没有定义键。定义此的EntityType的关键。
ToTheMoon.DAL.IdentityUserRole:的EntityType'IdentityUserRole没有定义键。定义此的EntityType的关键。
IdentityUserLogins:的EntityType:EntitySet的'IdentityUserLogins'是基于类型'IdentityUserLogin'不具有自定义键。
IdentityUserRoles:的EntityType:EntitySet的'IdentityUserRoles'是基于类型'IdentityUserRole'不具有自定义键。

我已经包含在我的模型中的一个类型 ApplicationUser 财产(?)。我认为,这是什么原因引起的:

公共ApplicationUser请求者{搞定;组; }

我非常清楚的事实是,在一般我应该明确标记某些与 [重点] 标注的关键,但我不相信这适用的错误消息所描述的是MVC5的电池包括登录系统的一部分课程。

我已经看到了一些有关这个错误所覆盖的方法(不记得它现在是什么人)所造成的,但我不这样做,让我在网上找到的其他所有问题都是对我没用

什么是AP preciated。

编辑:这里是我的情况下,它是pretty裸

公共类ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
    公共ApplicationDbContext():基地(DefaultConnection){}
}


解决方案

从问题的意见:

似乎增加了ApplicationUser请求者属性在不同的上下文中定义的实体。因为它是在不同的上下文这方面一无所知的所有实体身份的配置,因为这是成立由IdentityDbContext.OnModelCreating方法。

I'm writing C#/ASP.NET MVC5 web application, and I'm hitting the following errors when I try to do a new migration

ToTheMoon.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
ToTheMoon.DAL.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.

I have included a property(?) of type ApplicationUser in one of my models. I believe that this is what's causing it:

public ApplicationUser Requester { get; set; }

I am well aware of the fact that—in general—I should explicitly mark something as a key with the [Key] annotation, but I don't believe this applies as the error messages are describing classes that are part of MVC5's 'batteries included' login system.

I have seen something about this being caused by incorrectly overriding a method (can't remember what one it is now), but I'm not doing that, so all other questions that I've found online are useless to me.

Anything is appreciated.

EDIT: Here's my context, it's pretty bare.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection") { }
}

解决方案

From the comments in the Question:

It appears the entity that added the ApplicationUser Requester property is defined in a different context. Because it is in a different context that context knows nothing about the configurations for all of the Identity entities, since that is set up by the IdentityDbContext.OnModelCreating method.

这篇关于&QUOT;定义此的EntityType关键&QUOT。当包括类型ApplicationUser属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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