简体   繁体   English

EF6.1.1代码优先一对一/一个关系

[英]EF6.1.1 Code First One-to-Zero/One Relationship

I am trying to define these tables. 我正在尝试定义这些表。

_tbl1_
id      (PK)

_tbl2_
id      (PK)
tbl1_id (FK)

_tbl3_
id      (PK)
tbl1_id (FK)

...where I can navigate from Tbl1 to Tbl2 or Tbl3 ...我可以从Tbl1导航到Tbl2Tbl3

or back from Tbl2 or Tbl3 to Tbl1 . 或从Tbl2Tbl3返回Tbl1

While the association from Tbl2 or Tbl3 to Tbl1 are required, a record in Tbl2 or Tbl3 may not exist; 而需要从TBL2TBL3TBL1的关联,在TBL2TBL3的记录可能不存在; so Tbl1 records should be able to exist with our both or one of Tbl2 and Tbl3 . 因此Tbl1记录应该能够同时存在于我们的Tbl2Tbl3中

The design is this way to have optional relationships to two completely different entities from the perspective of Tbl1 . Tbl1的角度来看,这种设计具有对两个完全不同的实体具有可选关系的方式

class Tbl1
{
    [Key]
    public int Id { get; set; }
    public Tbl2 Tbl2 { get; set; }
    public Tbl2 Tbl3 { get; set; }
}

class Tbl2
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("Tbl1")]
    public int Tbl1Id { get; set; }
    public Tbl1 Tbl1 { get; set; }
}

class Tbl3
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("Tbl1")]
    public int Tbl1Id { get; set; }
    public Tbl1 Tbl1 { get; set; }
}

and fluent... 而且流利...

modelBuilder.Entity<Tbl2>()
    .HasRequired(e => e.Tbl1)
    .WithOptional(e => e.Tbl2);

modelBuilder.Entity<Tbl3>()
    .HasRequired(e => e.Tbl1)
    .WithOptional(e => e.Tbl3);

Using the above I get an error of: 使用上面我得到一个错误:

Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. 从属角色属性不是关键属性,从属角色多重性的上限必须为'*'。

Which seems to imply that the foreign key must be the record key as well. 这似乎暗示着外键也必须是记录键。

So, can I do this? 那么,我可以这样做吗? Can I not?. 我可以吗?

I am happy to implement with either attributes or fluent api or both. 我很高兴用属性或流利的api或两者来实现。

I think your ForeignKey attribute needs to refer to a column and be placed above a navigation property. 我认为您的ForeignKey属性需要引用一列,并放在导航属性上方。 eg: 例如:

[ForeignKey("Tbl1Id")]
public virtual Tbl1 tbl1 {get; set;}

Even if you define a navigation property on Tbl1 to Tbl2 and Tbl3, the foreign key annotation should be defined in the dependent classes (tbl2 and tbl3) not the principal class. 即使您在Tbl1到Tbl2和Tbl3上定义了导航属性,外键注释也应在相关类(tbl2和tbl3)中而不是主体类中定义。

I'm not sure if this will solve the overall problem, but maybe it will help. 我不确定这是否可以解决整体问题,但也许会有所帮助。

Good luck 祝好运

Just as in other questions of a similar vein; 就像其他类似问题一样; One to One or Zero can be accomplished by using the same property for the entity key and the foreign key to the principal entity on the dependent entity. 可以通过对实体密钥和从属实体上的主体实体使用相同的属性来实现一对一或零。

I don't see another way, and since creating a one to one or zero in SQL would require the foreign key to have a uniqueness constraint, the end result is pretty much the same. 我看不到另一种方法,因为在SQL中创建一对一或零将要求外键具有唯一性约束,所以最终结果几乎相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM