EF 4.1 - 模型关系
我正在尝试使用 EF 4.1 的 RC 版本创建一个快速的 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; }
}
插入新的 Race 时出现以下错误:
无法确定主体端 类型之间的关联 'rcommander.Models.Race' 和 'rcommander.Models.Address'。这 该协会的主要目的必须 使用任一显式配置 关系流畅的 API 或数据 注释。
难道它不应该自动将 RaceId 识别为 Races 表的主键,并将 AddressId 自动识别为 Addresses 表的 FK 吗?我错过了什么吗?
谢谢!
I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models:
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; }
}
and
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; }
}
I get the following error when trying to insert a new Race:
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.
Shouldn't it recognize RaceId as the primary key of the Races table and AddressId as the FK to the Addresses table automatically? Am I missing something?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里的问题是地址和种族之间的 1:1 关系!您可能想将其映射为 1:N,因此您需要将地址修改为:
如果您想使用 1:1 那么您不能在 Race 中使用 AddressId,但 Address 中的 AddressId 必须是 Race 的外键,因为实体框架可以实现1:1只是“共享”主键。
The problem here is 1:1 relation between Address and Race! You probably want to map it as 1:N so you need to modify address to:
If you want to use 1:1 then you can't use AddressId in Race but AddressId in Address must be foreign key of Race because entity framework can achive 1:1 only be "sharing" primary key.
对于一对一关系,需要在第二类中添加“[required]”属性。见下文:
For one-to-one relationship, you need to add "[required]" attribute in the second class. See below:
有一篇很好的文章:EF Code First CTP5 中的关联:第 2 部分 – 共享主键关联
http://weblogs.asp.net/manavi/archive/2010/12/19/entity -association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx
There is a good post: Associations in EF Code First CTP5: Part 2 – Shared Primary Key Associations
http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx
按照惯例,它将 Id 识别为主键。那么你需要做什么:
[Key]
属性表明它应该是 PrimaryKey如果你不想要这个,你需要将你的主键重命名为简单的
public int Id {得到;放; }
It recognizes Id as the primary key by convention. So what you need to do:
The
[Key]
attribute indicates that it should be the PrimaryKeyIf you don't want this, you need to rename your primary keys to simply
public int Id {get; set; }
我认为它也可以这样解决...我假设地址不需要与种族相关联,但种族必须始终与地址相关联。
我在患者和事件方面遇到了同样的问题,我用 InverseProperty 解决了它,这实际上与外键相同,但方向相反
I think it would be solved also like this... I assumed that an address is not required to be associated with a race, but a race must always be associated with an address.
I had the same problem with Patients and Incidents and i solved it with InverseProperty which is actually the same with foreign key, but the other direction
这里的问题似乎是 EntityFramework 无法识别外键在哪里,因为您在两个对象中都持有交叉引用。不确定您想要实现什么,我可能会建议这样的事情:
在第二个实体中跳过对 Race 的引用。
The problem here seems to be that EntityFramework can't recognize where the foreing key is, as you are holding cross references in both objects. Not being sure what you want to achieve, I may suggest something like this:
Skipping reference to Race in second entity.