简体   繁体   English

具有事务管理的实体框架批量插入-性能

[英]Entity Framework bulk insert with transaction management - Performance

I followed the answer in the following link for improving the performance of entity framework for bulk inserts and updates. 我遵循以下链接中的答案,以改善用于大容量插入和更新的实体框架的性能。

Improving bulk insert performance in Entity framework 在实体框架中提高批量插入性能

  1. Made save changes in batch of 1000 批量保存更改1000
  2. regeneration of the DbContext object after every save changes 每次保存更改后DbContext对象的重新生成

I got the performance boost as expected. 我得到了预期的性能提升。

But I have a requirement of rolling back the transaction while exception condition while performing bulk insert. 但是我需要在执行批量插入时在异常条件下回滚事务。

This becomes issue when we re initialize the DbContext object. 当我们重新初始化DbContext对象时,这成为问题。

Is there any other way to use the transaction and rollback with the same setup under the exception condition? 在异常情况下,还有其他方法可以通过相同的设置使用事务和回滚吗?

Any help will be appreciated 任何帮助将不胜感激

Transaction 交易

If you want to rollback changes, you will need to use a transaction. 如果要回滚更改,则需要使用事务。

You can share the same connection within all context you generate. 您可以在所生成的所有上下文中共享相同的连接。

Here is a small example: 这是一个小例子:

try
{
    var connection = new SqlConnection("[ConnectionString]");
    var trans = connection.BeginTransaction();

    while (condition)
    {
        using (TestContext context = new TestContext(connection))
        {
             // ...code..
        }
    }

    trans.Commit();
}
catch
{
    // ...code..
}

public class TestContext : DbContext
{
    public TestContext(SqlConnection connection) : base(connection, false)
    {

    }

    // ...code...
}

Bulk Insert 批量插入

You are not performing a BulkInsert . 您没有执行BulkInsert You are currently fixing a performance issue with the DetectChanges methods that's called every time you use the Add method. 当前,您正在解决每次使用Add方法时都会调用的DetectChanges方法的性能问题。

See: http://entityframework.net/improve-ef-add-performance 请参阅: http//entityframework.net/improve-ef-add-performance

If you have 50,000 entities to insert, you are still performing 50,000 database round-trip which is INSANELY slow. 如果您要插入50,000个实体,那么您仍在执行50,000个数据库往返,这确实非常慢。

Disclaimer : I'm the owner of the project Entity Framework Extensions 免责声明 :我是项目Entity Framework Extensions的所有者

This library is a paid library but let you to really perform BulkInsert. 该库是一个付费库,但可让您真正执行BulkInsert。

Only a few database round-trip will be required 只需几次数据库往返

Example

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Bulk Operations
context.BulkInsert(customers, options => {
   options => options.IncludeGraph = true;
});
context.BulkMerge(customers, options => {
   options.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});

EDIT: Answer Comment 编辑:答案评论

the proposed library indeed looks great. 提议的图书馆确实很棒。 But can you please elaborate regarding the transaction management with bulk operations? 但是,您能否详细说明批量操作的交易管理?

Sure, 当然,

Documentation: http://entityframework-extensions.net/transaction 文档: http : //entityframework-extensions.net/transaction

BulkSaveChanges 批量保存更改

As SaveChanges, BulkSaveChanges already save all entities within an internal transaction. 作为SaveChanges,BulkSaveChanges已经保存了内部事务中的所有实体。 So by default, there is nothing to do. 因此,默认情况下,没有任何事可做。

However, if you start a transaction within Entity Framework, BulkSaveChanges will honor it and will use this transaction instead of creating an internal transaction. 但是,如果您在Entity Framework中启动事务,BulkSaveChanges将兑现它,并将使用此事务而不是创建内部事务。

var transaction = context.Database.BeginTransaction();
try
{
    context.BulkSaveChanges();
    transaction.Commit();
}
catch
{
    transaction.Rollback();
}

Bulk Operations 批量操作

Bulk Operations such as BulkInsert, BulkUpdate, BulkDelete doesn't use a transaction by default. 默认情况下,BulkInsert,BulkUpdate,BulkDelete等批量操作不使用事务。 This is your responsibility to handle it. 这是您的责任。

If you start a transaction within Entity Framework, Bulk Operations will honor it. 如果您在实体框架内启动事务,则批量操作将兑现它。

var transaction = context.Database.BeginTransaction();
try
{
    context.BulkInsert(list1);
    context.BulkInsert(list2);
    transaction.Commit();
}
catch
{
    transaction.Rollback();
}

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

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