How to ExecuteSqlCommand in Entity Framework without it being contained in a transaction

久未见 提交于 2019-12-23 14:38:48

问题


I need to execute a stored procedure with Entity Framework.

Normally I call it like this:

this.Context.Database.ExecuteSqlCommand("EXEC edi_UploadTransmission");

However, this particular stored procedure includes accessing a linked server.

Since EF wraps ExecuteSqlCommand in a transaction, it is failing, as a linked server is not supported in a transaction (as far as I can tell).

Is there a way to execute this stored procedure with Entity Framework without it being in a transaction?


回答1:


Pass TransactionalBehavior.DoNotEnsureTransaction as the first parameter to the ExecuteSqlCommand method.

For example,

this.Context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "EXEC edi_UploadTransmission");



回答2:


My recommendation would be to simply not use EF for this part of your code. You can freely combine EF with straight ADO.NET code or with other ORMs such as Dapper or Chain.

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper



来源:https://stackoverflow.com/questions/36609208/how-to-executesqlcommand-in-entity-framework-without-it-being-contained-in-a-tra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!