使用实体框架 5
所以我有一个客户。客户可以有许多地址,但至少有一个。其中一个地址也将设置为主地址(必需)。我尝试了各种映射,但到目前为止,我在构建或为数据库播种时遇到错误。
顾客:
public class Customer
{
public int CustomerId { get; set;}
public String CustomerName { get; set; }
public int PrimaryAddressId { get; set; }
public virtual CustomerAddress PrimaryAddress { get; set; }
public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}
地址:
public class CustomerAddress : Address
{
public int CustomerAddressId { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
我的这部分映射工作正常。它在客户地址上。
this.HasRequired(c => c.Customer)
.WithMany(d => d.CustomerAddresses)
.HasForeignKey(c => c.CustomerId);
但是如何为在 Customer 中设置 PrimaryAddress 指定正确的映射?或者这是错误的方法?
谢谢
编辑 - 使用 Arnolds 和 LueTM 的答案:
这段代码现在可以工作了。
顾客:
public class Customer
{
public int CustomerId { get; set;}
public String CustomerName { get; set; }
// public int PrimaryAddressId { get; set; } created in mapping
public virtual CustomerAddress PrimaryAddress { get; set; }
public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}
地址:
public class CustomerAddress : Address
{
public int CustomerAddressId { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
客户映射:
modelBuilder.Entity<Customer>
.HasOptional(c => c.PrimaryAddress)
.WithOptionalDependent().Map(m => m.MapKey("PrimaryAddressId"));
modelBuilder.Entity<Customer>
.HasMany(c => c.CustomerAddresses)
.WithRequired(c => c.Customer)
.HasForeignKey(c => c.CustomerId)
.WillCascadeOnDelete(false);
我使用存储库来确保首先创建一个新地址并保存,然后将其设置为主地址并再次保存。存储库确保主要是“必需的”。