实体框架之间的关系是另一个字段而不是id

时间:2016-02-11 17:43:50

标签: entity-framework relationship foreign-key-relationship

我有两个班(表)
Person { id(primary key) , code, name, address, ...} Order {id(primary key) , order_number, customer, create_date, description, ...}

我想创建Person.codeOrder.customer之间的关系(一两个)。 如何首先在EF代码中创建该关系。

1 个答案:

答案 0 :(得分:0)

好的,它有一个简单的解决方案

[Table("Person")]
public partial class Person
{
    public long ID { get; set; }

    [Key()]
    [StringLength(10)]
    public string code { get; set; }

    [StringLength(100)]
    public string name { get; set; }

    [Column(TypeName = "text")]
    public string address{ get; set; }

    public ICollection<Order> Orders { get; set; }
}

和订单

[Table("Order")]
public partial class Order
{
    public long ID { get; set; }

    public int order_number { get; set; }

    [StringLength(10)]
    public string customer { get; set; }

    [Column(TypeName = "text")]
    public string description { get; set; }

    //...

    [ForeignKey("customer")]
    public Library Person { get; set; }
}

我将针对每个导航属性创建一个关于规范各种复合键的新问题。