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();
unitOfWork.ProcessY();
unitOfWork.PersistCase();
});
}
Unit of measurement:
class Unit
{
public PersistCase()
{
using (var dbContext = new CustomDbContext())
{
using (var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions() {
IsolationLevel = System.Transaction.IsolationLevel.ReadCommitted
}))
{
dbContext.Units.Add(thisUnit);
dbContext.SaveChanges();
CopyGeneratedId(thisUnit.AutoIncrementedId, dataTables);
SqlBulkCopy bulkCopy1 = new SqlBulkCopy(...);
...
bulkCopy1.WriteToServer(dataTables["#1"]);
SqlBulkCopy bulkCopy2 = new SqlBulkCopy(...);
...
bulkCopy2.WriteToServer(dataTables["#2"]);
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.