我正在尝试使用RC 4.1版本创建一个快速的ASP.NET MVC 3应用程序。我有两个型号:
public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } }
和
public class Address { public int AddressId { get; set; } public string Street { get; set; } public string StreetCont { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public virtual Race Race { get; set; } }
尝试插入新的比赛时,我收到以下错误:
Unable to determine the principal end
of an association between the types
‘rcommander.Models.Race’ and
‘rcommander.Models.Address’. The
principal end of this association must
be explicitly configured using either
the relationship fluent API or data
annotations.
不应该将RaceId作为Races表和AddressId的主键识别为地址表的FK吗?我缺少什么?
谢谢!
解决方法
这里的问题似乎是EntityFramework无法识别前导键的位置,因为您在两个对象中都保存交叉引用。不确定你想要实现什么,我可能会建议这样:
public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } } public class Address { public int AddressId { get; set; } public string Street { get; set; } public string StreetCont { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } }
在第二个实体中跳过参考。