0

我首先在我的应用程序中使用以下模型和 Entity Framework 6 代码。

public class Customer
{
    public Customer
    {

    }

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

    public virtual Address Address { get; set; }
}


public class Address
{
    public Address
    {

    }

    public int Id { get; set; }
    public string Street { get; set; }
    public int Number { get; set; }
    public int Country { get; set; }

    public virtual Customer Customer { get; set; }              
}

当我尝试保存它们时,出现以下错误:

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

4

1 回答 1

3

您需要指定外键关系。如此处所述,尝试[ForeignKey("CustomerId")]一遍又一public virtual Customer Customer { get; set; }遍地 [ForeignKey("AddressId")]添加public virtual Address Address { get; set; },并将这些 id 字段添加到模型中。如:

public class Customer
{
    public Customer
    {

    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int Addressid { get; set; }

    [ForeignKey("AddressId")]
    public virtual Address Address { get; set; }
}


public class Address
{
    public Address
    {

    }

    public int Id { get; set; }
    public string Street { get; set; }
    public int Number { get; set; }
    public int Country { get; set; }
    public int CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public virtual Customer Customer { get; set; }              
}
于 2013-10-27T21:21:07.350 回答