简体   繁体   中英

How do I ensure an idempotent database insert with Entity Framework 5?

I want to ensure that my database (SQL Server) updates are idempotent, similar in functionality to the blog post below, but using Entity Framework (v5, Database First ):

http://blogs.msdn.com/b/adonet/archive/2013/03/11/sql-database-connectivity-and-the-idempotency-issue.aspx

However, if I add an @Operation parameter to my insert proc, I get

"Error 2037: A mapping function bindings specifies a function ... but does not map the following function parameters: Operation"

(The @Operation would be a GUID, generated by the app, allowing the app to retry with the same GUID if it doesn't get a success response from the proc. The proc would write the GUID to a log table, and only perform the insert if the GUID doesn't exist - thus ensuring an idempotent transaction.)

Is there an elegant solution?

Release 6.1 of Entity Framework will solve this issue. (Thanks EF team - fabulous work.)

https://entityframework.codeplex.com/wikipage?title=Handling%20of%20Transaction%20Commit%20Failures%20

If I understood your query properly - How about applying a unique constraint on the column which holds the GUID and then if you don't get a success response back after EXECuting the proc, retry the insert.

Within the insert proc potentially use a try/catch or even query for the GUID value w/o Try/Catch and if it's there handle the exception or just exit the proc cleanly with a success message via the RETURN statement.

I don't think you'll need to do anything special on the EF side.

And one last thing, maybe wrap the CRUD insert/update statement inside a transaction to ensure that a value was in fact written like in the example provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM