实体框架一对一映射

时间:2018-07-25 18:27:23

标签: .net entity-framework

我有一个可以提供帐单或送货地址的客户。客户的BillingAddressId和ShippingAddressId对应于地址表上的AddressId。

我尝试了以下设置,但始终出现错误:

public class Customer
    [Key]
    public int CustomerId { get; set; }

    [Column("billingAddressId")]
    [ForeignKey("Address")]
    public int BillingAddressId { get; set; }

    [Column("shippingAddressId")]
    public int ShippingAddressId { get; set; }

    public virtual Address BillingAddress { get; set; }

    public virtual Address ShippingAddress { get; set; }

地址如下:

public class Address
    [Key]
    public int AddressId {get;set;}

    public virtual Customer Customer { get; set; }

我的模型构建器:

    modelBuilder.Entity<Customer>()
            .HasOptional(c => c.BillingAddress)
            .WithOptionalPrincipal(a => a.Customer);

但是我不断收到此错误The ForeignKeyAttribute on property 'BillingAddressId' on type '...Customer' is not valid. The navigation property 'Address' was not found on the dependent type '...Customer

0 个答案:

没有答案