EF 4.1 EntityType 没有键 - 复合
我刚刚使用 NuGet 升级到最新的 EF 4.1 代码优先,现在收到有关映射的错误。
我有这个类
public class UserApplication
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ApplicationId { get; set; }
[ForeignKey("ApplicationId")]
public virtual Application Application { get; set; }
public DateTime SubscribedDate { get; set; }
}
并且我收到此错误:
System.Data.Edm.EdmEntityType: : EntityType 'UserApplication' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet 'UserApplications' is based on type 'UserApplication' that has no keys defined.
但是我可以使用像这样的 Fluent API 使其工作
modelBuilder.Entity<UserApplication>().HasKey(obj => new {obj.UserId, obj.ApplicationId });
为什么 EF 没有拾取我使用注释定义的复合键? 我非常确定这曾经适用于较旧的 EF 4.1。
I have just upgraded to the latest EF 4.1 code-first using NuGet and now I am receiving an error regarding my mapping.
I have this class
public class UserApplication
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ApplicationId { get; set; }
[ForeignKey("ApplicationId")]
public virtual Application Application { get; set; }
public DateTime SubscribedDate { get; set; }
}
And I am getting this error:
System.Data.Edm.EdmEntityType: : EntityType 'UserApplication' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet 'UserApplications' is based on type 'UserApplication' that has no keys defined.
I can however make it work using the Fluent API like this
modelBuilder.Entity<UserApplication>().HasKey(obj => new {obj.UserId, obj.ApplicationId });
How come EF doesn't pick up the composite key that I have defined using annotations?
I am quite certain that this used to work with an older EF 4.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
适用于实体框架 6.1.3。我还添加了一个基本的
Application
类,但按原样使用了UserApplication
(DatabaseGeneratedOption 除外)。生成了以下 SQL:Works with Entity Framework 6.1.3. I also added a basic
Application
class, but used theUserApplication
as is (except for DatabaseGeneratedOption. This SQL was generated:我已经解决了这个问题。我刚刚添加了额外的关键列:
I've solved the issue. I've just added additional key columns: