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

外部キー関係を指定する必要があります。hereで述べたように、[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 に答える