FIX Entity Framework Error: New transaction is not allowed because there are other threads!

Bei verwendung des Entity Framework kommt es mal vor das man seine Daten in einer Schleife bearbeiten möchte.

Normalerweise macht man das so.






 


/* get data */
IQeryable<Item> items = context.Item.Where(i => i.Id == 2);
/* loop */
foreach(Item item in items){
    item.title = 'new'
    context.SaveChanges();
}
1
2
3
4
5
6
7

Jetzt kommts.

Zeile 6: context.SaveChanges() "entity framework new transaction is not allowed because there are other threads"

IQeryable verhält sich hier wie eine Transaktion, wodurch keine Änderungen gespeichert werden können.

Dieses Lässt sich ganz einfach lösen mit IList.


 






/* get data */
IList<Item> items = context.Item.Where(i => i.Id == 2).ToList<Item>();
/* loop */
foreach(Item item in items){
    item.title = 'new'
    context.SaveChanges();
}
1
2
3
4
5
6
7

Das wars schon.