EF conflicts competing with SaveChanges ()

I am creating a batch processing system. Parties Unitsare included in quantities from 20-1000. Each Unitis essentially a hierarchy of models (one main model and many child models). My task is to save each model hierarchy in the database as one transaction (each hierarchy commits or rolls back). Unfortunately, EFI could not process two parts of the model hierarchy because of their ability to contain thousands of records.

To do this, I decided to configure the SqlBulkCopyprocessing of these two models with a potentially high counter and EFprocess the remaining inserts (and referential integrity).

Batch cycle:

foreach (var unitDetails in BatchUnits)
{
  var unitOfWork = new Unit(unitDetails);
  Task.Factory.StartNew(() =>
    {
      unitOfWork.ProcessX(); // data preparation
      unitOfWork.ProcessY(); // data preparation
      unitOfWork.PersistCase();
    });
}

Unit of measurement:

class Unit
{
  public PersistCase()
  {
    using (var dbContext = new CustomDbContext())
    {
      // Need an explicit transaction so that 
      // EF + SqlBulkCopy act as a single block
      using (var scope = new TransactionScope(TransactionScopeOption.Required,
        new TransactionOptions() {
          IsolationLevel = System.Transaction.IsolationLevel.ReadCommitted
        }))
      {
        // Let EF Insert most of the records
        // Note Insert is all it is doing, no update or delete
        dbContext.Units.Add(thisUnit);
        dbContext.SaveChanges();  // deadlocks, DbConcurrencyExceptions here

        // Copy Auto Inc Generated Id (set by EF) to DataTables
        // for referential integrity of SqlBulkCopy inserts
        CopyGeneratedId(thisUnit.AutoIncrementedId, dataTables);

        // Execute SqlBulkCopy for potentially numerous model #1
        SqlBulkCopy bulkCopy1 = new SqlBulkCopy(...);
        ...
        bulkCopy1.WriteToServer(dataTables["#1"]);

        // Execute SqlBulkCopy for potentially number model #2
        SqlBulkCopy bulkCopy2 = new SqlBulkCopy(...);
        ...
        bulkCopy2.WriteToServer(dataTables["#2"]);

        // Commit transaction
        scope.Complete();
      }
    }
  }
}

. IsolationLevel ReadCommitted, EF INSERT Tasks.

IsolationLevel ReadUncommitted (, , , SELECTs), DbConcurrencyExceptions.

DbConcurrencyExceptions Entity Framework, , ReadUncommitted , EF , .

UPDATE

, INSERTS:

http://connect.microsoft.com/VisualStudio/feedback/details/562148/how-to-avoid-using-scope-identity-based-insert-commands-on-sql-server-2005

-, , Linq To SQL, Microsoft , scope_identity(). , , SQL Server, Entity Framework.

+5
1

: http://connect.microsoft.com/VisualStudio/feedback/details/562148/how-to-avoid-using-scope-identity-based-insert-commands-on-sql-server-2005

EF. Linq To SQL, ( SELECT ).

Linq To Sql, :

, Linq to SQL SQL ​​. , - Id. SQL:

exec sp_executesql N'INSERT INTO [dbo]. [Order] ([Colum1], [Column2]) (@p0, @p1)

SELECT [t0]. [Id] FROM [dbo]. [Order] AS [t0] WHERE [t0]. [Id] = (SCOPE_IDENTITY()) ', N' @p0 int, @p1 int, @p0 = 124, @p1 = 432

, , SCOPE_IDENTITY() , 'SELECT SCOPE_IDENTITY()', SQL SELECT Id, , SCOPE_IDENTITY(). , . , .

+3

All Articles