实体框架 - 多个多对多关系

时间:2012-01-18 19:27:04

标签: asp.net asp.net-mvc asp.net-mvc-3 entity-framework

我试图描述映射到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次映射。并且可能用第二个覆盖第一个,而用最后一个覆盖第二个。

问题:

如何告诉EF关于同一两个表之间的多个多对多映射?

1 个答案:

答案 0 :(得分:1)

我想通了,这是因为我在第一个和第三个声明中使用了相同的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");
        }
    );