1
ObjectContext.ExecuteFunction<GetStoredProcedure_Result>("StoredProcedure", parameter);

Does this code cover transaction automatically (by Entity Framework)? Or should I add transaction in the stored procedure as well?

Please provide any links.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ajay Bhasy
  • 1,920
  • 1
  • 26
  • 38

1 Answers1

1

In EF6 every stored procedure call is wrapped in Transaction. But for the earlier versions of EF it will not cover automatically. You need to specify TransactionScope as following...

using (TransactionScope transaction = new TransactionScope())
{
   //your code here
}

For more on transaction follow MSDN.

I think this link and this link can help you in your case.

Community
  • 1
  • 1
PaulShovan
  • 2,140
  • 1
  • 13
  • 22
  • 2
    Again, if you really want to have transnational DB logic then it's always recommended to implement the same at DB end inside your procedure code logic rather in ADO.NET or in application code. – Rahul Jan 04 '16 at 12:11
  • But according to this link it says the opposite (From what I understand) https://msdn.microsoft.com/en-us/data/dn456843.aspx Wanted to make sure. – Ajay Bhasy Jan 04 '16 at 12:12
  • You should think of isolationlevel (https://msdn.microsoft.com/en-us/library/system.transactions.isolationlevel.aspx). – PaulShovan Jan 04 '16 at 12:20
  • 1
    EF implicitly supports transactions. If you are doing SaveChanges EF implicitly wraps commands in a db transaction such as SqlTransaction. By default, EF uses DbTransaction to take care of ops on a single instance of a db conn. Also EF supports over riding of the trans, so I guess for your case as gypsy suggesed you need to use TransactionScope(). – VivekDev Jan 04 '16 at 12:21
  • So in my code while calling ExecuteFunction, if an error occurs in the execution of Stored Procedure, it doesn't roll back? And I need to implement transactionScope in C# or Transaction in SQL to achieve this? Is this what is implemented in the answer? – Ajay Bhasy Jan 04 '16 at 12:25
  • EF's default transaction isolation level can cover the `SaveChanges` operation. But in you case where you are executing a function you need to use `TransactionScope` @wintersolider – PaulShovan Jan 04 '16 at 12:28
  • @wintersolider have you got your answer? – PaulShovan Jan 05 '16 at 07:48