Code First Fluent API:关系映射

时间:2018-12-14 03:34:50

标签: entity-framework ef-code-first data-annotations ef-fluent-api

  

需要您的帮助来重复配置相同类型的EF,   使用数据注释或Fluent API

public class Order
{
    [Key]
    public long Id {get; set;}

    public virtual long BillingAddressId { get; set;}
    public virtual Address BillingAddress { get; set;}

    public virtual long ShippingAddressId { get; set;}
    public virtual Address ShippingAddress { get; set;}   

    public virtual ICollection<LineItem> LineItems { get; set; }
    public virtual ICollection<ShippingLine> ShippingLines { get; set; }     

}

public class Customer
{
     [Key]
     public long CustomerId { get; set;}
     public virtual Address Address { get; set;}    
}
  

尝试以下操作,但出现此错误:   无法确定类型的组合主键顺序   “客户” 使用    ColumnAttribute (请参见http://go.microsoft.com/fwlink/?LinkId=386388)或    HasKey方法(请参见http://go.microsoft.com/fwlink/?LinkId=386387)   指定组合主键的顺序。

     

请查看并建议其他配置,而不仅仅是   客户

       modelBuilder.Entity<Order> (  )
            .HasRequired ( cr => cr.BillingAddress )
            .WithMany (  )
            .HasForeignKey ( cr => cr.BillingAddressId )
            .WillCascadeOnDelete ( true );

        modelBuilder.Entity<Order>()
            .HasRequired(cr => cr.ShippingAddress)
            .WithMany()
            .HasForeignKey(cr => cr.ShippingAddressId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<LineItem>()
            .HasRequired(l => l.Order)
            .WithMany(cr => cr.LineItems)
            .HasForeignKey(l => l.OrderId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<ShippingLine>()
            .HasRequired(c => c.Order)
            .WithMany(l => l.ShippingLines)
            .HasForeignKey(l => l.OrderId).WillCascadeOnDelete(false);

        modelBuilder.Entity<Customer>()
            .HasOptional(c => c.Address)
            .WithRequired(cr => cr.Customer);

0 个答案:

没有答案