Occasionally, when calling SaveChanges()
on an Entity Framework ObjectContext
throws an exception within a try
... catch
block, you may find that a subsequent call to SaveChanges
only throw
s the same exception. How can you get round this?
Basically, the problem stems from the fact that you made changes to your ObjectContext
which the database then rejected (most likely situation, but not exclusively. There may be connectivity issues etc., etc.). However, just because you caught the exception doesn't mean that your ObjectContext
isn't still in an invalid state. Unless you've made several changes to your ObjectContext
object before calling SaveChanges()
you can simply call AcceptAllChanges()
on the object context before carrying on. This basically tells the ObjectContext
to let go of all the changes made to it and basically clears the slate, allowing you to make new changes to the object and subsequently save the changes.
E.g. (Heavily abbreviated code simply to demonstrate the principle. Please don't make obvious comments about its value.)
ObjectContext context = new Entities();
Client newClient = Client.CreateClient
("johndoe", "John", "Doe", ... various other properties);
try{
context.AddToClients(newClient);
context.SaveChanges();
}
catch(UniqueConstraintViolationException)
{
context.AcceptAllChanges();
}
CodeProject