如何在实体框架中执行SQLCommand,而不包含在事务中 [英] How to ExecuteSqlCommand in Entity Framework without it being contained in a transaction

查看:547
本文介绍了如何在实体框架中执行SQLCommand,而不包含在事务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



通常我称之为:

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

但是,这个特定的存储过程包括访问链接的服务器。



由于EF在事务中包裹 ExecuteSqlCommand ,所以它是失败的,因为事务中不支持链接服务器(据我所知)。



有没有办法在Entity Framework中执行这个存储过程,而不是在事务中?

解决方案

TransactionalBehavior.DoNotEnsureTransaction 作为 ExecuteSqlCommand 方法的第一个参数。 p>

例如,

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


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?

解决方案

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

For example,

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

这篇关于如何在实体框架中执行SQLCommand,而不包含在事务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆