无法确定类型之间关联的主要结束

时间:2013-05-30 17:25:03

标签: c# entity-framework

我收到了这个错误:

  

无法确定之间关联的主要结束   类型CustomerDetail和Customer。

以下是我的CustomerCustomerDetail型号

[Table("CUSTOMER")]
public class Customer
{
    [Required]
    [Column("CUSTOMER_ID")]
    public int Id {get; set;}

    [Column("FIRST_NAME")]
    public string FirstName {get; set;}
    // other fields  

    public virtual CustomerDetail customerDetail {get; set;}
}

[Table("CUSTOMER_DETAIL")]
public class CustomerDetail
{
    [Required]
    [Column("CUSTOMER_DETAIL_ID")]
    public int Id {get; set;}
    // other fields

    public virtual Customer Customer {get; set;} 
}

CustomerCustomerDetail的关系为1:1。

1 个答案:

答案 0 :(得分:10)

我认为您必须在ForeignKey属性上指定Customer关系,该属性映射到实体上存在的关键属性。

[Table("CUSTOMER_DETAIL")]
public class CustomerDetail
{
    [Required]
    [Column("CUSTOMER_DETAIL_ID")]
    public int Id {get; set;}
    // other fields

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

This question指的是一个不同的错误,但其目标与您要实现的目标类似。