Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Update Exception

I just noticed that when a SqlException occurs while updating data using Entity Framework, all subsequent calls to SaveChanges will fail because the Entity that caused the exception is still in the "queue" to be saved.

This just stung us on a live system as many users were not able save their data due to some failed update with error 'string or binary data would be truncated'. We just had to resort to recycling the application pool.

How can I remove/reset the offending object and let other updates go through?

EDIT: Better yet, what is the best way of handling Sql Exeptions that occur during insert/update?

like image 710
Alfero Chingono Avatar asked Jul 13 '26 09:07

Alfero Chingono


2 Answers

First essential rule of EF - ObjectContext is unit of work and because of that you have to deal it with this way. If you share context among users you did so big mistake that you should immediately turn off the application and don't let your users use it unless you make a fix. I wrote the answer where I describing two patterns the context is implementing - unit of work and identity map. If you share the context among concurrent users you make the result of each operation undeterministic. You can't say if user has a fresh / commited data and you can't say if you saved everything the user was modifying in the single transaction.

Now the context uses transaction internally. Each SaveChanges saves every modified record (from all concurrent users if you share the context) in the transaction. Single error and whole transaction is rolled back. Once you start to use context per logical operation (request or action in the case of web application) you can show user the data and let him do modification or you can simply try it again (in case of dealing with locking issues). This of course doesn't solve problems where you do not validate user input because that is the bug in application which must be fixed. If you don't have a validation you can't show the user what is wrong with data.

like image 115
Ladislav Mrnka Avatar answered Jul 17 '26 17:07

Ladislav Mrnka


You can protect yourself from that situation by following the Unit of Work pattern with your context.

In that situation, each update from a different user would use its own context...so if an error was present in one it wouldn't affect the others.

Take a look at:

Using Repository and Unit of Work patterns with Entity Framework 4.0

The Unit of Work will also help you understand how to recover from an error.

Since a single Unit of Work is, in essence, a single transaction you can simply roll back the transaction and notify the user (and allow them to modify their data and try again).

like image 28
Justin Niessner Avatar answered Jul 17 '26 17:07

Justin Niessner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!