C# 无法确定类型之间关联的主要端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16842363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:56:03  来源:igfitidea点击:

Unable to determine the principal end of an association between the types

c#entity-framework

提问by Harrison Brock

I'm getting this error:

我收到此错误:

Unable to determine the principal end of an association between the types CustomerDetail and Customer.

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

Here is my Customerand CustomerDetailmodels

这是我的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;} 
}

Customerto CustomerDetailhas a 1:1 relation.

CustomerCustomerDetail了1:1的关系。

采纳答案by mclark1129

I think that you have to specify a ForeignKeyrelation on the Customerproperty that maps to the key property exists on the entity.

我认为您必须ForeignKeyCustomer映射到实体上存在的键属性的属性上指定一个关系。

[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 questionrefers to a different error, but has a similar goal to what you are trying to achieve.

这个问题指的是一个不同的错误,但与您要实现的目标有相似的目标。