locked
Implementing bulk save with Entity Framework RRS feed

  • Question

  • Hi,

     

    I thought about implementing bulk save with Entity Framework, at least in situations where I know I'm only inserting new data.

    I was wondering if anyone has tried to do this?

     

    My thought was to catch the SQL queries at some point, and aggregate them. Then sort them by table or something like that, and convert them to bulk inserts.

     

    Any thoughts?

     

    thanks

    YaronT

    Wednesday, April 13, 2011 7:34 AM

Answers

  • Hello YaronT,

     

    Welcome to the EF Forum!

    As I know, sometimes you simply have to mix models. Perhaps use SqlBulkCopy for this part of your repository (since this plugs directly into the bulk-copy API), and Entity Framework for some of the rest. And if necessary, a bit of direct ADO.NET. Ultimately the goal is to get the job done.

    For a perfect example of how to do bulk inserts with LINQ to Entities, see http://archive.msdn.microsoft.com/LinqEntityDataReader. It is a wrapper that allows easy use of SqlBulkCopy.

    I don't know whether this is what you want, if not, the following is another way to bulk insert, please see this answered thread.

     

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Monday, April 25, 2011 5:35 AM
    Thursday, April 14, 2011 7:49 AM

All replies

  • Hello YaronT,

     

    Welcome to the EF Forum!

    As I know, sometimes you simply have to mix models. Perhaps use SqlBulkCopy for this part of your repository (since this plugs directly into the bulk-copy API), and Entity Framework for some of the rest. And if necessary, a bit of direct ADO.NET. Ultimately the goal is to get the job done.

    For a perfect example of how to do bulk inserts with LINQ to Entities, see http://archive.msdn.microsoft.com/LinqEntityDataReader. It is a wrapper that allows easy use of SqlBulkCopy.

    I don't know whether this is what you want, if not, the following is another way to bulk insert, please see this answered thread.

     

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Monday, April 25, 2011 5:35 AM
    Thursday, April 14, 2011 7:49 AM
  • @Jackie Sun

    You've blatantly copied the top two answers from the StackOverflow question you link to and passed them off as your own.   They're good answers but it's dishonest to imply that you wrote them yourself.

    http://stackoverflow.com/questions/1609153/how-to-do-a-bulk-insert-linq-to-entities


    Thursday, August 2, 2012 6:35 PM
  • You could try ef bulk insert extension. Very easy to use


    • Edited by maxlego Thursday, March 6, 2014 9:46 PM
    Thursday, March 6, 2014 9:46 PM
  • ZZZ Projects offers 2 kinds of bulk operations via the Entity Framework Extensions Library. They increase drastically your performance over the SaveChanges method from Entity Framework.

    BulkSaveChanges

    The first way is via our main feature, the BulkSaveChanges method which literally replaces the SaveChanges method. You can expect to save your entities a minimum of 10-15 times faster with thousands of entities. This method supports all kinds of associations and entity types (TPC, TPH, and TPT).

    using (var ctx = new DbContextEntities())
    {
    	// ... Make any changes to your DbContext ...
    	ctx.BulkSaveChanges();
    }

    Bulk Operations

    The second ways is via Bulk Operations methods (BulkDelete, BulkInsert, BulkUpdate and BulkMerge) which increases even more the performance and allows customization for many settings like what's the primary key to use.

    using (var ctx = new DbContextEntities())
    {
    	// ... Make any changes to your DbContext ...
    
    	// Use Entity Framework Setting
    	ctx.BulkUpdate(list);
    
    	// OR customize your settings 
    	ctx.BulkUpdate(list, operation => operation.ColumnPrimaryKeyExpression = column => column.ID);
    }



    • Edited by ZZZ Projects Monday, December 1, 2014 12:26 AM
    • Proposed as answer by ZZZ Projects Monday, December 1, 2014 12:26 AM
    Wednesday, April 2, 2014 4:16 PM