Entity Framework - множественные отношения «многие ко многим» - PullRequest
1 голос
/ 18 января 2012

Я пытаюсь описать сопоставление с 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; }
}

OnModelСоздание:

        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 Ответ

1 голос
/ 18 января 2012

Я понял это, потому что я использовал тот же 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");
        }
    );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...