1. <i id='MLgKL'><tr id='MLgKL'><dt id='MLgKL'><q id='MLgKL'><span id='MLgKL'><b id='MLgKL'><form id='MLgKL'><ins id='MLgKL'></ins><ul id='MLgKL'></ul><sub id='MLgKL'></sub></form><legend id='MLgKL'></legend><bdo id='MLgKL'><pre id='MLgKL'><center id='MLgKL'></center></pre></bdo></b><th id='MLgKL'></th></span></q></dt></tr></i><div id='MLgKL'><tfoot id='MLgKL'></tfoot><dl id='MLgKL'><fieldset id='MLgKL'></fieldset></dl></div>
      2. <small id='MLgKL'></small><noframes id='MLgKL'>

        • <bdo id='MLgKL'></bdo><ul id='MLgKL'></ul>
        <legend id='MLgKL'><style id='MLgKL'><dir id='MLgKL'><q id='MLgKL'></q></dir></style></legend>
      3. <tfoot id='MLgKL'></tfoot>

        将当前事务传递给 DbCommand

        Pass current transaction to DbCommand(将当前事务传递给 DbCommand)

        <small id='YJK55'></small><noframes id='YJK55'>

            <tfoot id='YJK55'></tfoot>

              • <bdo id='YJK55'></bdo><ul id='YJK55'></ul>

                  <i id='YJK55'><tr id='YJK55'><dt id='YJK55'><q id='YJK55'><span id='YJK55'><b id='YJK55'><form id='YJK55'><ins id='YJK55'></ins><ul id='YJK55'></ul><sub id='YJK55'></sub></form><legend id='YJK55'></legend><bdo id='YJK55'><pre id='YJK55'><center id='YJK55'></center></pre></bdo></b><th id='YJK55'></th></span></q></dt></tr></i><div id='YJK55'><tfoot id='YJK55'></tfoot><dl id='YJK55'><fieldset id='YJK55'></fieldset></dl></div>
                    <tbody id='YJK55'></tbody>
                  <legend id='YJK55'><style id='YJK55'><dir id='YJK55'><q id='YJK55'></q></dir></style></legend>

                1. 本文介绍了将当前事务传递给 DbCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个使用 ASP.NET Core 2.1 和 EF Core 2.1 的项目.虽然大部分查询和命令都使用EF,但有些单元需要直接调用存储过程.

                  I'm working on a project using ASP.NET Core 2.1 and EF Core 2.1. Although most of queries and commands use EF, some units needs to call stored procedures directly.

                  我不能使用FromSql,因为它需要基于实体模型的结果集.

                  I can't use FromSql, because it needs results set based on entity models.

                  假设我们有这些方法:

                  public Task CommandOne(DbContext context)
                  {
                      Entity entity = new Entity
                      {
                          Name = "Name"
                      };
                      context.DbSet<Entity>().Add(entity);
                      return context.SaveChangesAsync();
                  }
                  
                  public async Task CommandTwo(DbContext context)
                  {
                      DbCommand command = context.Database.GetDbConnection().CreateCommand();
                      command.CommandText = "storedProcName";
                      command.CommandType = System.Data.CommandType.StoredProcedure;
                  
                      using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
                      {
                          // read result sets
                      }
                  }
                  

                  如果我像这样在一个事务中调用两个命令:

                  If I call both commands in one transaction like this:

                  public async Task UnitOfWork(DbContext dbContext)
                  {
                      using (var transaction = await dbContext.Database.BeginTransactionAsync())
                      {
                          await CommandOne(dbContext);
                          await CommandTwo(dbContext);
                      }
                  }
                  

                  这个异常会发生:

                  BeginExecuteReader 要求命令有一个事务,当分配给命令的连接处于挂起的本地事务中.该命令的 Transaction 属性尚未初始化.

                  BeginExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

                  不得不提,它并不像command.Transaction = ...那么简单.这需要与 EF 使用的事务不同的 DbTransaction.

                  I have to mention, it's not as simple as command.Transaction = .... This requires DbTransaction which differs from the transaction EF uses.

                  我已经囤了一个月了!有什么解决方法吗?非常感谢.

                  I've stocked with this for a month! Is there any workaround for this? Thank you so much.

                  推荐答案

                  不得不提,它不像command.Transaction = ....那么简单,这需要DbTransaction,这与EF使用的事务不同.

                  I have to mention, it's not as simple as command.Transaction = .... This requires DbTransaction which differs from the transaction EF uses.

                  其实是这样的.您只需要参考 Microsoft.EntityFrameworkCore.Relational 程序集并添加

                  Actually it is. All you need is a reference to Microsoft.EntityFrameworkCore.Relational assembly and add

                  using Microsoft.EntityFrameworkCore.Storage;
                  

                  访问 GetDbTransaction 扩展方法:

                  to get access to GetDbTransaction extension method:

                  if (context.Database.CurrentTransaction != null)
                      command.Transaction = context.Database.CurrentTransaction.GetDbTransaction();
                  

                  这篇关于将当前事务传递给 DbCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                    • <small id='TFSUW'></small><noframes id='TFSUW'>

                      1. <legend id='TFSUW'><style id='TFSUW'><dir id='TFSUW'><q id='TFSUW'></q></dir></style></legend>

                        <i id='TFSUW'><tr id='TFSUW'><dt id='TFSUW'><q id='TFSUW'><span id='TFSUW'><b id='TFSUW'><form id='TFSUW'><ins id='TFSUW'></ins><ul id='TFSUW'></ul><sub id='TFSUW'></sub></form><legend id='TFSUW'></legend><bdo id='TFSUW'><pre id='TFSUW'><center id='TFSUW'></center></pre></bdo></b><th id='TFSUW'></th></span></q></dt></tr></i><div id='TFSUW'><tfoot id='TFSUW'></tfoot><dl id='TFSUW'><fieldset id='TFSUW'></fieldset></dl></div>
                          <tbody id='TFSUW'></tbody>

                          <tfoot id='TFSUW'></tfoot>
                            <bdo id='TFSUW'></bdo><ul id='TFSUW'></ul>