简体   繁体   English

使用Entity Framework 5将大量数据从旧数据库移动到新数据库

[英]Move large amount of data from old database to new database using Entity Framework 5

I'm creating an application to move data from old to new database (different schema). 我正在创建一个应用程序,以将数据从旧数据库移动到新数据库(不同的架构)。 I'm using Visual Studio 2013 , C# , Entity Framework 5 , Microsoft SQL Server 2012 . 我正在使用Visual Studio 2013C#Entity Framework 5Microsoft SQL Server 2012 This table, Customer , has more than 40 thousand records . Customer这个表有4万多条记录

private void TransferCustomer()
{
    int counter = 0;

    // Load all old customers
    var oldCustomers = _oldRockDale.customers;

    foreach (var oldCustomer in oldCustomers)
    {
        // Create new customer
        ...

        // Modify something
        ...

        // Add to collection
        <New_database_entity>.Customers.Add(newCustomer);

        // Insert to database for each 1000 records
        counter++;
        if (counter % 1000 == 0)
        {
            <New_database_entity>.SaveChanges();
        }
    }

    // Insert the rest to database
    <New_database_entity>.SaveChanges();
}

Here is my problem: this function runs slower and slower. 这是我的问题: 此功能运行的越来越慢。 For the first 1000 records, it's just about 20 - 30 seconds. 对于前1000条记录,大约只有20-30秒。 But it becomes much slower as it goes. 但是随着它的发展,它变得越来越慢。 Then, it takes more than 1 minutes to reach 2000. 然后,需要超过1分钟才能达到2000。

My questions are: 我的问题是:

  • Why does it run slower and slower? 为什么它运行的越来越慢?
  • Is there any better way to transfer a large amount of data like this? 有没有更好的方法可以传输大量数据呢?

One more information: as I observe in Output window: 更多信息:正如我在“ Output窗口中观察到的:

  • Only 1 line saying that thread exited with code 0 . 只有1行表示该线程以代码0退出
  • After that, there are many lines saying thread exited with code 259 . 在那之后,有很多行说线程以代码259退出

Thank you so much for your help. 非常感谢你的帮助。

I think this is linked to the growth of the DbContext. 我认为这与DbContext的增长有关。

You can take advantage of the following posts: 您可以利用以下职位:

Basically you have to insert by parts of 100 rows for example, and reset (as set again, that is: _context = new SomeContext(); ) the context between each insert. 基本上,您必须插入例如100行的部分,然后重置(再次设置,即_context = new SomeContext(); )每次插入之间的上下文。

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

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