Execute Reader requires command to have transaction when connection assigned to command is in pending local trans
See original GitHub issueExecuteReader 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@viktorz You typically need to tell EF to use the transaction. For example:
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.
Maybe this can help? https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/ErikEJ.EntityFrameworkCore.SqlServer.SqlQuery/DbContextExtensions.cs