2012-01-18 5 views
1

関係:エンティティFramwork - 複数の多対多(簡体字)私は、次の2つのエンティティ間の複数の多対多のrealtionshipsを作成するために、EFへのマッピングを記述しようとしています

製品:

public class Product 
    { 
     [Key] 
     public int ProductID { get; set; } 

     public string ProductName { get; set; } 
     public decimal Price { get; set; } 

     public virtual ICollection<Transaction> Transactions { get; set; } 
     public virtual ICollection<Transaction> RefundTransactions { get; set; } 
     public virtual ICollection<Transaction> VoidTransactions { get; set; } 
    } 

取引:

public class Transaction 
{ 
    [Key] 
    public int TransactionID { get; set; } 

    public virtual ICollection<Product> Products { get; set; } 
    public virtual ICollection<Product> RefundProducts { get; set; } 
    public virtual ICollection<Product> VoidProducts { get; set; } 
} 

OnModelCreating:

 modelBuilder.Entity<Transaction>() 
      .HasMany(m => m.Products) 
      .WithMany(t => t.Transactions) 
      .Map(m => 
      { 
       m.ToTable("Transaction_Product_Mapping"); 
       m.MapLeftKey("TransactionID"); 
       m.MapRightKey("ProductID"); 
      } 
     ); 

     modelBuilder.Entity<Transaction>() 
      .HasMany(transaction => transaction.VoidProducts) 
      .WithMany(t => t.VoidTransactions) 
      .Map(m => 
      { 
       m.ToTable("Transaction_Void_Product_Mapping"); 
       m.MapLeftKey("TransactionID"); 
       m.MapRightKey("VoidProductID"); 
      } 
     ); 

     modelBuilder.Entity<Transaction>() 
      .HasMany(m => m.RefundProducts) 
      .WithMany(t => t.Transactions) 
      .Map(m => 
      { 
       m.ToTable("Transaction_Refund_Product_Mapping"); 
       m.MapLeftKey("TransactionID"); 
       m.MapRightKey("RefundProductID"); 
      } 
     ); 

が例外:

Type Transaction_Products is not defined in namespace Nautix_EPOS.Models 

は今、私は3回別々のマッピングを定義していますので、これはかもしれないと思います。そしておそらく第1のものを第2のものに上書きし、第2のものを最後に上書きする。

質問:

私は同じ2つのテーブル間の複数の多対多のマッピングについてEFを伝えることができますどのように?

答えて

1

私はそれを理解しました。なぜなら、最初と3番目の宣言で同じt.Transactionsを使用したからです。私は使用する必要がありますt.RefundTransactions

modelBuilder.Entity<Transaction>() 
     .HasMany(m => m.Products) 
     .WithMany(t => t.Transactions) 
     .Map(m => 
     { 
      m.ToTable("Transaction_Product_Mapping"); 
      m.MapLeftKey("TransactionID"); 
      m.MapRightKey("ProductID"); 
     } 
    ); 

    modelBuilder.Entity<Transaction>() 
     .HasMany(transaction => transaction.VoidProducts) 
     .WithMany(t => t.VoidTransactions) 
     .Map(m => 
     { 
      m.ToTable("Transaction_Void_Product_Mapping"); 
      m.MapLeftKey("TransactionID"); 
      m.MapRightKey("VoidProductID"); 
     } 
    ); 

    modelBuilder.Entity<Transaction>() 
     .HasMany(m => m.RefundProducts) 
     .WithMany(t => t.RefundTransactions) 
     .Map(m => 
     { 
      m.ToTable("Transaction_Refund_Product_Mapping"); 
      m.MapLeftKey("TransactionID"); 
      m.MapRightKey("RefundProductID"); 
     } 
    ); 
関連する問題