实体框架 CTP 5 一对一映射
我有两个表:
Requirement
RequirementId - PK
Fixture
FixtureId - PK
RequirementId - FK / NULLABLE / Unique Constraint
一个夹具只能有 1 个要求,其他夹具不能引用相同的要求。 Fixture 的 Requirement 并不是强制性的,它是可选的。
我所做的是,在 Sql Server 中,我在 Fixture 表中的 RequirementId 列上放置了唯一约束。如何在 Entity Framework CTP 5 中设置此映射?
另外,每个实体是否可以具有双向导航属性?
public class Fixture
{
public int FixtureId { get; set; }
public Requirement Requirement { get; set; }
}
public class Requirement
{
public int RequirementId { get; set; }
public Fixture Fixture { get; set; }
}
也许我的理解都是错误的,所以任何建议都会很好。 提前致谢
I have two tables:
Requirement
RequirementId - PK
Fixture
FixtureId - PK
RequirementId - FK / NULLABLE / Unique Constraint
A Fixture can only have 1 Requirement, no other Fixture should be able to reference the same Requirement. It is not mandatory for a Fixture to have a Requirement, its optional.
What i have done is, in Sql Server i have placed a Unique constraint on the RequirementId column in the Fixture table. How do I setup the mapping for this in Entity Framework CTP 5 ?
Also would it be possible to have a bidirectional navigation property on each entity?
public class Fixture
{
public int FixtureId { get; set; }
public Requirement Requirement { get; set; }
}
public class Requirement
{
public int RequirementId { get; set; }
public Fixture Fixture { get; set; }
}
Maybe im getting this all wrong, so any advice would be great.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所追求的称为“一对一外键关联”,并且像 Ladislav 提到的那样,EF 本身并不支持。但是,我在 本文。
What you are after is called One-to-One Foreign Key Associations and like Ladislav mentioned is not natively supported by EF. However, I showed how to implement it with Code First in this article.
不幸的是,当前版本的 EF 无法使用唯一约束。在EF中实现1:0..1映射的唯一方法就是“共享PK”。这意味着如果主要实体是 Fixture,它将以 FixtureId 作为其 PK,并且 Requirement 将以 FixtureId 作为其 PK 和 FK 到 Fixture。
Unfortunatelly current version of EF is not able to work with unique constraint. The only way how to achieve 1:0..1 mapping in EF is "sharing PK". It means that if main entity is Fixture it will have FixtureId as its PK and Requirement will have FixtureId as its PK and FK to Fixture.