tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

DbUpdateConcurrencyException “client wins” and “store wins” resolution helpers

11 Jul 2014 2 mins Entity Framework

Whenever I’m dealing with DbUpdateConcurrencyException I don’t what is “client wins” and “store wins” look like. I can at the end figure it out because I know how the ObjectStateManager/ChangeTracker works and I kind of know where I’m heading. But going slowly though IntelliSense isn’t what I like to do.

In the old days of ObjectContext and OptimisticConcurrencyException it was easy. All you’ve had to do was use RefreshMode and “refresh”. But that’s past. The DbUpdateConcurrencyException era is here I can’t do anything about it. Except I actually can (if I skip the idea of beign stubborn and using ObjectContext). I can write myself helpers. 😮)

static class DbUpdateConcurrencyExceptionExtensions
{
	public static void ClientWins(this DbUpdateConcurrencyException exception)
	{
		var entry = exception.Entries.First();
		entry.OriginalValues.SetValues(entry.GetDatabaseValues());
	}

	public static async Task ClientWinsAsync(this DbUpdateConcurrencyException exception)
	{
		var entry = exception.Entries.First();
		entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync().ConfigureAwait(false));
	}

	public static void StoreWins(this DbUpdateConcurrencyException exception)
	{
		var entry = exception.Entries.First();
		entry.Reload();
	}

	public static Task StoreWinsAsync(this DbUpdateConcurrencyException exception)
	{
		var entry = exception.Entries.First();
		return entry.ReloadAsync();
	}
}

Now as I wrote the implementation, basically twice, I think I’ve learned it enough that I don’t need these anymore. 😃 Maybe it will help others while looking for these strategies.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.