EF6使用Fluent API编写第一个复合外键

时间:2018-11-22 07:44:59

标签: entity-framework-6 foreign-keys ef-code-first one-to-one

我无法在代码优先EF6流利的API上创建一对一(零)外键关系

我的实体是

 public class Invoice
    {
        public int AccountID { get; set; }
        public int InvoiceID { get; set; }
        public decimal Amount { get; set; }
        public Refund Refund { get; set; }
        internal static void ConfigureModel(DbModelBuilder modelBuilder)
        {
              modelBuilder.Entity<Invoice>()
                       .HasKey(e => new { e.AccountID, e.InvoiceID });
        }
    }

public class Refund 
    {
        public int AccountID { get; set; }
        public int RefundID { get; set; }
        public decimal Amount { get; set; }
        public int InvoiceID { get; set; }
        public Invoice Invoice { get; set; }
        internal static void ConfigureModel(DbModelBuilder modelBuilder)
       {
             modelBuilder.Entity<Refund>()
                      .HasKey(e => new { e.AccountID, e.RefundID });
             modelBuilder.Entity<Refund>()
                .HasRequired(e => e.Invoice )
               // .HasForeignKey(e => new { e.AccountID, e.InvoiceID});
       }
    }

我无法使用流畅的API在退款表上定义组合外键

如果我更改

public Refund Refund { get; set; } 

public ICollection<Refund> Refund{ get; set; } 

然后我可以将此关系添加为

modelBuilder.Entity<Refund>()
                .HasRequired(e => e.Invoice)
                .WithMany(e => e.Refund)
                .HasForeignKey(e => new { e.AccountID, e.InvoiceID});

0 个答案:

没有答案