简体   繁体   English

如何在Entity Framework模型中正确定义一对多关系?

[英]How to correctly define a one-to-many relationship in Entity Framework models?

I have two entities in database: customer and user and building a one-to-many relationship between them, so 1 customer -> many users. 我在数据库中有两个实体:客户和用户,并在它们之间建立一对多关系,因此1个客户 - >许多用户。

This is my customer model: 这是我的客户模型:

public class Customer
{           
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public ICollection<User> Users { get; set; }
}

What would be the correct User model, I mean with Customer property and CustomerId property or just CustomerId? 什么是正确的用户模型,我的意思是Customer属性和CustomerId属性或只是CustomerId? So this: 所以这:

public class User
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public Customer Customer { get; set; }

    [ForeignKey("Customer")]
    public int CustomerId { get; set; }
}

.. or this?: .. 或这个?:

public class User
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("Customer")]
    public int CustomerId { get; set; }
}

Actually it should be like the code example below. 实际上它应该像下面的代码示例。 You should specify what is local primary key, remote key and if there is navigation property ( Customer ), then You should specify "How should it recognize the object" => based on the this or that key of Customer ( CustomerId ). 您应该指定什么是本地主键,远程键以及是否有导航属性( Customer ),那么您应该根据Customer( CustomerId )的this或that键指定“它应该如何识别对象”=>。

public class User
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    //other properties

    [ForeignKey("CustomerId")]
    public Customer Customer { get; set; }

    public int CustomerId { get; set; }
}

If You are not interested in having navigation property, You can simply avoid that to have only: 如果您对导航属性不感兴趣,您可以简单地避免只有:

public class User
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    //other properties

    public int CustomerId { get; set; }
}

More info can be found there: http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx 更多信息可以在那里找到: http//www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx


Note: You do not need to specify [ForeignKey] attribute, if You are using Navigation Property with the same name (eg Customer & CustomerId ). 注意:如果您使用的是具有相同名称的Navigation Property (例如CustomerCustomerId ),则无需指定[ForeignKey]属性。

If there are multiple references to the same class (eg You have User that will have exactly 2 Customers (CustomerA, CustomerB), then You must specify following: 如果对同一个类有多个引用(例如,您的用户将拥有正好2个客户(CustomerA,CustomerB),那么您必须指定以下内容:

public class User
{       
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    //other properties


    [ForeignKey("CustomerIdA")]
    public Customer CustomerA { get; set; }
    public int CustomerIdA { get; set; }

    [ForeignKey("CustomerIdB")]
    public Customer CustomerB { get; set; }
    public int CustomerIdB { get; set; }
}

In the first case you have a correctly set navigational property between User and Customer. 在第一种情况下,您在User和Customer之间具有正确设置的导航属性。 In the second case, you cannot navigate from User to Customer, and you have a Foreign Key attribute that point to a non-existent property. 在第二种情况下,您无法从“用户”导航到“客户”,并且您具有指向不存在的属性的“ Foreign Key属性。

You should, however, invert the order of the ForeingKeyAttribute to make it clearer (both ways are OK, though): 但是,您应该反转ForeingKeyAttribute的顺序以使其更清晰(但两种方式都可以):

[ForeignKey("CustomerId")]
public Customer Customer { get; set; }

public int CustomerId { get; set; }

If you won't need User.Customer , you can skip setting up the navigational property altogether. 如果您不需要User.Customer ,则可以完全跳过设置导航属性。 The same applies to Customer.Users , you can skip setting up that navigational property if it's not needed. 这同样适用于Customer.Users ,如果不需要,您可以跳过设置该导航属性。

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

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