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

entity-framework entity-framework-6

Question

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
5
4/13/2016 8:54:28 PM

Accepted Answer

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

For example,

this.Context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "EXEC edi_UploadTransmission");
11
10/28/2016 7:26:01 AM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow