简体   繁体   English

无法在连接表实体框架中两次保存相同的记录

[英]Can't save the same record twice in the join table Entity Framework

I have 2 tables, Invoice and Product , and I'm using EF to save data to my database我有 2 个表, InvoiceProduct ,我正在使用 EF 将数据保存到我的数据库

The two classes and my context class are as following:这两个类和我的上下文 class 如下:

public class Invoice
{
        public int Id { get; set; }
        public string InvoiceNumber { get; set; }
        public double TotalPrice { get; set; }
        public List<Product> Products { get; set; }
}
public class Product
{
        public int Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public List<Invoice> Invoices { get; set; }
}
public class DB : DbContext
{
    public DB() : base("MyConnectionstringName") { }

    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<Product> Products { get; set; }
}

Finally, this is how I save data to the database:最后,这是我将数据保存到数据库的方式:

public string Create(Invoice invoice, List<Product> products)
{
        foreach (var item in products)
        {
            invoice.Products.Add
                (db.Products.Find(item.id));
        }
        db.Invoices.Add(invoice);
        db.SaveChanges();
        ...
        return "Invoice was successfully saved";
}

The problem is, if I have the same Product twice in the products , I gets added to the join table in the database, only once.问题是,如果我在products中有两次相同的 Product ,我只会被添加到数据库中的连接表中一次。 For example, if a customer buys two Pepperoni pizza which has the Id of 5, after saving, there will be only of row of data in InvoiceProduct pointing to this particular invoice, and ProductId of 5.例如,如果客户购买了两个 ID 为 5 的Pepperoni pizza ,保存后, InvoiceProduct中将只有一行数据指向该特定发票,并且 ProductId 为 5。

Everything is fine up until db.Invoices.Add(invoice);一切都很好,直到db.Invoices.Add(invoice); , cause when I check invoice in debug mode I can see several of the same item were added to its Products property, but when it gets saved to the database, it only get saved once. ,因为当我在调试模式下检查invoice时,我可以看到几个相同的项目被添加到它的Products属性中,但是当它被保存到数据库时,它只被保存一次。

And it works just fine with several of different products for the same invoice.它适用于同一张发票的几种不同产品。

What am I not seeing here?我在这里没有看到什么?

In sql relational world you have the Products table, the Invoice table and the ProductsInvoices table that represents a many-to-many relationship.在 sql 关系世界中,您有 Products 表、Invoice 表和代表多对多关系的 ProductsInvoices 表。 In that table you have the reference for an id of a product and an id of an invoice.在该表中,您有产品 ID 和发票 ID 的参考。 When you add it twice EF understand that you already have the relation you want and does nothing.当你添加它两次时,EF 明白你已经拥有了你想要的关系并且什么都不做。

In order to solve this I would create other class named InvoiceItem having a Product, an Invoice and a Quantity:为了解决这个问题,我将创建另一个名为 InvoiceItem 的 class,它有一个产品、一个发票和一个数量:

public class Invoice
{
        public int Id { get; set; }
        public string InvoiceNumber { get; set; }
        public double TotalPrice { get; set; }
        public List<InvoiceItem> Items { get; set; }
}

public class InvoiceItem
{
        public int Id { get; set; }
        public int Quantity { get; set; }
        public Product Product { get; set; }
}

public class Product
{
        public int Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public List<InvoiceItem> Invoices { get; set; }
}

public class DB : DbContext
{
    public DB() : base("MyConnectionstringName") { }

    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<InvoiceItem> InvoiceItems { get; set; }
    public DbSet<Product> Products { get; set; }
}

It is recommended that you also add the copy of the item description and price of the Product in the InvoiceItem object, because if in the future the user changes the price or name of the product it doesn't alter the Invoice that were generated in the past.建议您还在 InvoiceItem object 中添加产品的项目描述和价格的副本,因为如果将来用户更改产品的价格或名称,它不会更改在过去的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM