Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public void BulkSaveChanges_DatabaseWins(DbContext ctx)
{
bool saveFailed;
do
{
saveFailed = false;
try
{
ctx.BulkSaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Update the values of the entity that failed to save from the store
ex.Entries.Single().Reload();
}
} while (saveFailed);
}
public void BulkSaveChanges_ClientWins(DbContext ctx)
{
bool saveFailed;
do
{
saveFailed = false;
try
{
ctx.BulkSaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Update original values from the database
var entry = ex.Entries.Single();
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}
} while (saveFailed);
}
public void BulkSaveChanges_CustomResolution(CurrentContext ctx)
{
public void BulkSaveChanges_CustomResolution(CurrentContext ctx)
{
bool saveFailed;
do
{
saveFailed = false;
try
{
ctx.BulkSaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Get the current entity values and the values in the database
// as instances of the entity type
var entry = ex.Entries.Single();
var databaseValues = entry.GetDatabaseValues();
if (entry.Entity is EntitySimple_Concurrency)
{
var clientEntity = (EntitySimple_Concurrency) entry.Entity;
var databaseEntity = (EntitySimple_Concurrency) databaseValues.ToObject();
// Choose an initial set of resolved values. In this case we
// make the default be the values currently in the database.
var resolvedEntity = (EntitySimple_Concurrency) databaseValues.ToObject();
// Have the user choose what the resolved values should be
resolvedEntity.IntColumn = clientEntity.IntColumn + 100;
// ... merge all columns...
// Update the original values with the database values and
// the current values with whatever the user choose.
entry.OriginalValues.SetValues(databaseValues);
entry.CurrentValues.SetValues(resolvedEntity);
}
}
} while (saveFailed);
}
public void BulkUpdate_DatabaseWins<T>(CurrentContext ctx, List<T> list) where T : class
{
try
{
ctx.BulkUpdate(list);
}
catch (DbBulkOperationConcurrencyException ex)
{
// DO nothing (or log), keep database values!
}
}
public void BulkUpdate_StoreWins<T>(CurrentContext ctx, List<T> list) where T : class
{
try
{
ctx.BulkUpdate(list);
}
catch (DbBulkOperationConcurrencyException ex)
{
// FORCE update store entities
ctx.BulkUpdate(list, operation => operation.AllowConcurrency = false);
}
}
public void BulkUpdate_CustomResolution<T>(CurrentContext ctx, List<T> list) where T : class
{
try
{
ctx.BulkUpdate(list);
}
catch (DbBulkOperationConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
ObjectStateEntry objectEntry;
if (entry is EntitySimple_Concurrency)
{
var clientEntry = (EntitySimple_Concurrency) entry;
var databaseEntry = ctx.EntitySimple_Concurrencys.Single(x => x.ID == clientEntry.ID);
// merge properties like you want
clientEntry.IntColumn = databaseEntry.IntColumn;
}
}
// FORCE update store entities
ctx.BulkUpdate(list, operation => operation.AllowConcurrency = false);
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment