Search code examples
c#sql-serverentity-frameworkstored-procedurestransactions

Lock running from EF and not from Management studio?


I have a stored procedure for archiving some tables. It runs for ~30 minutes (first run, moving 20+ million records). If I start the script from management studio, I can run other querys and anything including the tables under archiving while it is running.

However, if I run the script from code (C#, EF) I can not get any results from the server, not even from management studio. I think (hope) this must be some connection string tweak but don't know what I'm looking for. What is the difference between mmstudio and ef ExecuteSqlCommand?

The c# code that runs the stored procedure:

SqlParameter daysparam = new SqlParameter("@Day", days);
using (ReportsContext rcx = new ReportsContext())
   {
       rcx.Database.CommandTimeout = 60 * 40;
       rcx.Database.ExecuteSqlCommand("Archive @Day", daysparam);
   }

The connection string:

connectionString="data source=localhost\SQLSERVER2014;initial catalog=****_dev;persist security info=True;user id=sa;password=***;MultipleActiveResultSets=True;App=EntityFramework"

Solution

  • The solution was to add TransactionalBehavior.DoNotEnsureTransaction as the first parameter of the ExecuteSqlCommand:

    rcx.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "Archive @Day", daysparam);