I am using Entity Framework on Azure. After makes a few changes to my data I call the DbContext.SaveChanges
.
In my code, Messages
belong to Conversations
. On receiving a message I add it to the Messages table and create its parent in the Conversations table. If I get two messages come in at once with the same parent, there is a possibility of double inserting values with the same primary key into the Conversations table.
The process:
I'll call some "insert conversation if it doesn't exist code":
if (this.Context.Conversations.SingleOrDefault(fc => fc.ConversationId == conversation.ConversationId) == null)
{
Context.Entry(conversation).State = EntityState.Added; //public DbEntityEntry Entry(object entity);
}
Later on I'll call this.Context.SaveChanges
I've been reading SQL Azure and Entity Framework Connection Fault Handling and I'm pretty sure I want to implement a Retry Policy With Transaction Scope.
How do I go about wrapping
SaveChanges
in a retry policy Some thoughts: How do I go about wrappingSaveChanges
in a retry policy. Will this retry the code I ran earlier that checked if the conversation existed? How do I make my retry policy retry for non-transient faults (like PK violations)?If a retry policy isn't possible , what is the entity framework approach to burying the "create or edit if exists" logic as far down as possible when saving a context? Is it best to just call a stored procedure in this special case?