Skip to content

Commit e3fb712

Browse files
Update dbbulkoperationconcurrency-exception.md
1 parent 5e27f7d commit e3fb712

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

docs/pages/troubleshooting/dbbulkoperationconcurrency-exception.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,86 @@ permalink: dbbulkoperationconcurrency-exception
55
---
66

77
{% include template-h1.html %}
8+
9+
## Problem
10+
11+
You execute a method from the Entity Framework Extensions library, and the following error is thrown:
12+
13+
- Type: DbBulkOperationConcurrencyException
14+
15+
{% include template-exception.html message='A concurrency exception has occured. Entities may have been modified or deleted since entities were loaded.' %}
16+
17+
## Solution
18+
19+
### Cause
20+
21+
Another thread have already performed the operation.
22+
23+
### Fix
24+
25+
There is three possible resolution:
26+
27+
- Database Win
28+
- Client Win
29+
- Custom Resolution
30+
31+
#### Database Win
32+
{% highlight csharp %}
33+
public void BulkUpdate_DatabaseWins<T>(CurrentContext ctx, List<T> list) where T : class
34+
{
35+
try
36+
{
37+
ctx.BulkUpdate(list);
38+
}
39+
catch (DbBulkOperationConcurrencyException ex)
40+
{
41+
// DO nothing (or log), keep database values!
42+
}
43+
}
44+
{% endhighlight %}
45+
46+
#### Client Win
47+
{% highlight csharp %}
48+
public void BulkUpdate_StoreWins<T>(CurrentContext ctx, List<T> list) where T : class
49+
{
50+
try
51+
{
52+
ctx.BulkUpdate(list);
53+
}
54+
catch (DbBulkOperationConcurrencyException ex)
55+
{
56+
// FORCE update store entities
57+
ctx.BulkUpdate(list, operation => operation.AllowConcurrency = false);
58+
}
59+
}
60+
{% endhighlight %}
61+
62+
#### Custom Resolution
63+
{% highlight csharp %}
64+
public void BulkUpdate_CustomResolution<T>(CurrentContext ctx, List<T> list) where T : class
65+
{
66+
try
67+
{
68+
ctx.BulkUpdate(list);
69+
}
70+
catch (DbBulkOperationConcurrencyException ex)
71+
{
72+
foreach (var entry in ex.Entries)
73+
{
74+
ObjectStateEntry objectEntry;
75+
76+
if (entry is EntitySimple_Concurrency)
77+
{
78+
var clientEntry = (EntitySimple_Concurrency) entry;
79+
var databaseEntry = ctx.EntitySimple_Concurrencys.Single(x => x.ID == clientEntry.ID);
80+
81+
// merge properties like you want
82+
clientEntry.IntColumn = databaseEntry.IntColumn + 303;
83+
}
84+
}
85+
86+
// FORCE update store entities
87+
ctx.BulkUpdate(list, operation => operation.AllowConcurrency = false);
88+
}
89+
}
90+
{% endhighlight %}

0 commit comments

Comments
 (0)