简体   繁体   English

如何确保使用Entity Framework 5插入幂等数据库?

[英]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 ): 我想确保我的数据库(SQL Server)更新是幂等的,功能类似于下面的博客文章,但使用实体框架(v5, 数据库优先 ):

http://blogs.msdn.com/b/adonet/archive/2013/03/11/sql-database-connectivity-and-the-idempotency-issue.aspx 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 但是,如果我在插入过程中添加@Operation参数,我会得到

"Error 2037: A mapping function bindings specifies a function ... but does not map the following function parameters: Operation" “错误2037:映射函数绑定指定函数...但不映射以下函数参数:操作”

(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.) (@Operation将是一个GUID,由应用程序生成,允许应用程序使用相同的GUID重试,如果它没有从proc获得成功响应.proc将GUID写入日志表,并且只执行如果GUID不存在,则插入 - 从而确保幂等事务。)

Is there an elegant solution? 有优雅的解决方案吗?

Release 6.1 of Entity Framework will solve this issue. 实体框架6.1版将解决此问题。 (Thanks EF team - fabulous work.) (感谢EF团队 - 精彩的工作。)

https://entityframework.codeplex.com/wikipage?title=Handling%20of%20Transaction%20Commit%20Failures%20 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. 如果我正确理解了您的查询 - 如何在保存GUID的列上应用唯一约束,然后如果在执行proc之后没有得到成功响应,请重试插入。

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. 在insert proc中可能使用try / catch或甚至查询GUID值没有Try / Catch,如果它在那里处理异常或只是通过RETURN语句通过成功消息干净地退出proc。

I don't think you'll need to do anything special on the EF side. 我不认为你需要在EF方面做任何特别的事情。

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. 最后一件事,可能是将CRUD插入/更新语句包装在事务中,以确保实际上写入的值与提供的示例一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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