Entity Framework Writing Mass Data to Database

时间:2015-09-14 16:09:26

标签: c# database entity-framework linq entity-framework-6

I've got a database which amongst others has two tables of data:-

Table 1

  • ProductID
  • ProductName
  • ProductDescription
  • IsVisible
  • IsDeleted

Table 2 - ProductPriceID - ProductID - LocationID - Price

Table 2 can hold many prices at different locations for each product in Table 1. I'm reading from a CSV file where the product details are listed in the first columns followed by 15 columns of price values for 15 locations.

I have found that with some nearly 10,000 products being imported each time, that writing this file to the database by first writing the product, and then writing a list of the 15 prices to Table 2, 10000 times over slows the import down HUGELY. It slows it down by up to 2.5x compared to 'attempting' to write in a list of 10000 products first, followed by the some 132,000 product prices. Having 2 writes to the database massively speeds up the whole process, as the lag time is incurred at the database so writing 2 times instead of 20,000 times is much easier.

I've created to lists of the Database types for each object and added the data to each and this is fine. The problem is the ProductID in Table 2. Entity Framework doesn't return this until I call

context.Products.Add(productList);
context.Save();

But at the point this is saved, the list of product prices has already been created but without the relevant ProductID values. When it saves, it crashes because of the foreign key constraint.

Is there anyway with Entity Framework to get the ProductID, that will be assigned to this product without writing each product to the database first? Minimum numbers of database calls is crucial here.

I have the option of re-parsing all the data from the file, but I'm also not keen on this, as its extra processing time. The structure of the file will not be able to be changed.

1 个答案:

答案 0 :(得分:0)

我权衡了所有选项,结果是我们做到这一点的最佳方式,就是将所有产品写入一个列表,将所有产品价格与已知产品代码一起写入另一个列表。

然后,我们将产品列表保存到数据库,然后在将产品价格列表保存到数据库之前,通过这些产品进行迭代,以针对与本地列表匹配的产品代码恢复ProductID。

2保存到数据库,并从数据库中调用一次,我们将47分钟的数据导入减少到3分钟。

感谢大家的帮助!