简体   繁体   English

EF4:定义多个外键

[英]EF4: Define multiple foreign keys

So I have a table people which is like this 所以我有一个这样的桌子人

Id    Name
1     John
2     Mike
3     Sophie

And I have a table Calls 我有一张桌子电话

Id    IdReceptor    IdRequired
1     1             2
2     1             1
3     2             3
4     3             1

Basically a person answers a telephone call, that person is the receptor, the one on the phones requires to talk with another person, it can be the same person who answered, or other, so we have this table design, IdReceptor and IdRequired both are foreign keys to people 基本上一个人接电话,那个人是接收者 ,电话上的一个人需要与另一个人交谈,可以是同一个人,也可以是另一个人,所以我们有此表设计, IdReceptorIdRequired都是外键的

How can I model this using EF4 Code First? 如何使用EF4代码优先对此建模?

For example like so: 例如这样:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Call
{
    public int Id { get; set; }

    [ForeignKey("Receptor")]
    public int IdReceptor { get; set; }
    [ForeignKey("Required")]
    public int IdRequired { get; set; }

    public Person Receptor { get; set; }
    public Person Required { get; set; }
}

You can introduce collections in Person if you want and add additional mapping with annotations or Fluent API or you can make the Person navigation properties virtual if you want lazy loading. 如果需要,可以在Person引入集合,并使用批注或Fluent API添加其他映射,或者,如果要延迟加载,可以使Person导航属性为virtual But the code above is a simple solution. 但是上面的代码是一个简单的解决方案。 EF will detect two one-to-many relationships by convention. EF将按照惯例检测两个一对多的关系。

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

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