Fastest Way of Inserting in Entity Framework

前端 未结 30 2204
鱼传尺愫
鱼传尺愫 2020-11-21 05:23

I\'m looking for the fastest way of inserting into Entity Framework.

I\'m asking this because of the scenario where you have an active TransactionScope a

30条回答
  •  遥遥无期
    2020-11-21 05:43

    Another option is to use SqlBulkTools available from Nuget. It's very easy to use and has some powerful features.

    Example:

    var bulk = new BulkOperations();
    var books = GetBooks();
    
    using (TransactionScope trans = new TransactionScope())
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager
        .ConnectionStrings["SqlBulkToolsTest"].ConnectionString))
        {
            bulk.Setup()
                .ForCollection(books)
                .WithTable("Books") 
                .AddAllColumns()
                .BulkInsert()
                .Commit(conn);
        }
    
        trans.Complete();
    }
    

    See the documentation for more examples and advanced usage. Disclaimer: I am the author of this library and any views are of my own opinion.

提交回复
热议问题