【发布时间】:2011-10-22 05:11:11
【问题描述】:
我刚刚使用 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。
【问题讨论】:
-
和你有同样的问题。你找到解决方案了吗?
-
不,我没有。最后,我决定使用 DbModelBuilder 定义所有键。
-
同样的问题,有趣的是我有两个带有复合键的类,一个可以编译,另一个没有..
-
看来这在 EF 6 中仍然是一个问题。
标签: entity-framework-4.1 composite-key