views:

31

answers:

2

I have this service which is Singleton and Single threaded and serves bunch of low volume clients. It uses Entity Framework and Data in SQL Server.

If any one of the client's request to Save Data fails, all the subsequent requests are being failed as every time it is trying to save the original failed data object.

Is there is any way to Undo changes to EF data when save fails?

Thanks in Advance

+1  A: 

Entity-models / data-contexts / etc are best handled as units of work. If you need to cancel it, simply discard the context and start with a new one. And if you succeed, discard it anyway! Each request should really be using separate data-contexts, otherwise you can get a range of problems:

  • threading (although it sounds you've avoided this by making it single-threaded)
  • data growth (there is an identity manager; every row you touch stays around; multiple times, in fact)
  • general isolation etc
  • connection lifetime management (hogging an open connection)
  • etc
Marc Gravell
Yes I realized that too late.. but the problem is that if you dispose object context and trying to connect the database, the performance is too bad. So we decided to keep the Object context alive. Trying to find a way we can roll back changes in case of a failure.
Bhuvan
@Bhuvan - that "performance is bad" claim usually means you are over-querying data. I've done **lots** of work where the data-context is very tightly scoped, and it is perfectly performant.
Marc Gravell
A: 

The Answer to the Question is "You Cannot discard Changes to the Context" instead one has to discard ObjectContext as Marc explained.

Bhuvan