【发布时间】:2017-05-28 08:06:59
【问题描述】:
我的代码是:
public class UserGroupUser
{
public int UserGroupUserId { get; set; }
public string UserId { get; set; }
[Key, ForeignKey("UserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
当我尝试创建控制器时出现此错误:
EntityType 'IdentityUserLogin' 没有定义键。定义键 此实体类型。
EntityType 'IdentityUserRole' 没有定义键。定义键 此实体类型。
EntityType 'IdentityUserLogins' 基于 'IdentityUserLogin' 没有定义键。
EntityType: EnititySet 'IdentityUserRoles' 基于 没有定义键的“IdentityUserRole”。
但是我可以在我的数据库中的所有表的 SQL 服务器管理中看到键。 我该如何解决这个问题?
【问题讨论】:
-
您必须使用
Id属性或[Key]属性为您的EF 实体指定主键。看起来您正在使用 ASP.NET Identity,对吗?有的话关注stackoverflow.com/questions/28531201/… -
不要在导航属性上使用
[Key]。在 UserId 属性上尝试[Key, ForeignKey("ApplicationUser ")]。
标签: entity-framework ef-code-first