question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execute Reader requires command to have transaction when connection assigned to command is in pending local trans

See original GitHub issue

ExecuteReader requires command to have transaction when connection assigned to command is in pending local trans

Example code

using (SchoolDataContext db = new SchoolDataContext())
{
    using(using var Transaction = db.Database.BeginTransaction())
    {
        SudentDtls SudentDtls = (from p in db.SudentDtls where p.SudentId == SudentId && p.StatusId == (int)EStatus.ActiveStatus select p).FirstOrDefault();
        SudentDtlsHistory SudentHistory = (from p in db.SudentDtlsHistory where p.SudentId == SudentId && p.StatusId == (int)EStatus.ActiveStatus select p).FirstOrDefault();
 
        SudentDtls = Automaper<SudentDtlsHistory, SudentDtls>(SudentHistory, SudentDtls);
        SudentDtls.ModifiedBy = UserId; 
        SudentDtls.ModifiedDate = DateTime.Now; 
        db.SaveChanges(); 

        MStudentData data = SqlQueryExtensions.SqlQuery<MStudentData>(db, "GetStudentData");
        if (data != null)
        {
                        
        }
        Transaction.Commit();
    }
}

SqlQueryExtensions: Class at SchoolFrameWork SqlQueryExtension method, asking for the current transaction but how to supply transaction to SqlQuery.

Stack Trace

at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at SchoolFrameWork.SqlQueryExtensions.SqlQuery[T](DbContext db, String sql, Boolean IsList, Object[] parameters) in D:\School_DataModified\SchoolDashboard\SchoolFrameWork\SqlQueryExtensions.cs:line 14
   at SchoolInfrastructure.Implementation.MyProfileHistory.IsApprovalRequired(SchoolDataContext context, Int32 EmployeeId) in D:\School_DataModified\SchoolDashboard\SchoolInfrastructure\Implementation\MyProfileHistory.cs:line 29
   at SchoolInfrastructure.Implementation.MyProfileHistory.InsertEmployeeData(MMyProfile myProfile, Int32 RoleId) in D:\School_DataModified\SchoolDashboard\SchoolInfrastructure\Implementation\MyProfileHistory.cs:line 72

version information

EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .Net Core 2.1 Operating system: Windows 10 IDE: Visual Studio 2019 16.10.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Aug 28, 2021

@viktorz You typically need to tell EF to use the transaction. For example:

dbContext.Database.UseTransaction(transaction);

See the Transactions documentation for more info.

However, I see you are also using an EF execution strategy. This complicates use of transactions. See Execution strategies and transactions for more info.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - ExecuteReader requires command to have transaction ...
ExecuteReader requires command to have transaction when connection assigned to command is in pending local trans · Subscribe to RSS.
Read more >
ExecuteReader requires command to have transaction when ...
Coding example for the question ExecuteReader requires command to have transaction when connection assigned to command is in pending local trans-C#.
Read more >
Executescalar requires the command to have a transaction
ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The ...
Read more >
ExecuteReader requires the command to have a ...
ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.
Read more >
ExecuteReader requires the command to have a ...
ExecuteReader requires the command to have a transaction #8038 ... It throws this exception. My Question is that how can we Pass the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found