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

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

        为什么我需要手动打开这个 .NET Core 数据库连接,

        时间:2023-06-06

          <legend id='4PYoS'><style id='4PYoS'><dir id='4PYoS'><q id='4PYoS'></q></dir></style></legend>
              <bdo id='4PYoS'></bdo><ul id='4PYoS'></ul>
              • <tfoot id='4PYoS'></tfoot>

                    <tbody id='4PYoS'></tbody>

                  <small id='4PYoS'></small><noframes id='4PYoS'>

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

                  本文介绍了为什么我需要手动打开这个 .NET Core 数据库连接,而通常 Dapper 会自动打开它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用 async/await 将一些记录插入到数据库中.如果我不手动打开连接,我会收到 Exception.如果我添加一些代码来打开连接,那么一切正常.

                  这是当前代码(抛出异常):

                  using (var connection = new SqlConnection(ConnectionString)){var tasks = new List();sellUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));等待 Task.WhenAll(tasks);}...私有异步任务 InsertUrlAsync(IDbConnection 连接,字符串 url){const string query = "INSERT INTO ..";return await connection.ExecuteAsync(query, new { .. });}

                  异常:

                  <块引用>

                  消息:System.InvalidOperationException:操作无效.连接已关闭.

                  但是当我将代码更改为以下内容时,它起作用了:

                  var tasks = new List();等待 connection.OpenAsync();sellUrls.ForEach(....) .. 等等...

                  类似的问题:

                  • Task.WhenAll,连接正在关闭
                  • SqlConnection 在 using 语句中意外关闭

                  解决方案

                  Dapper 为您打开连接;但它也会在工作完成后关闭它.

                  现在,您正在使用一个连接运行两个独立的 async 任务.

                  <块引用>

                  soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));

                  两个任务同时运行.当一项任务的工作完成时,它关闭连接.但是其他任务仍在运行,它不再有权访问打开的连接,因此出现了您提到的异常.

                  如您所说,如果您自己打开连接,Dapper 不会关闭它,一切正常.

                  顺便说一下,在并发使用连接实例时,可能会出现意想不到的问题.请参阅此问题了解更多详情.

                  I'm trying to use async/await to insert a number of records into a database. If I don't manually open the connection, I get an Exception. If I add some code to open the connection, then everything works ok.

                  Here's the current code (which throws an Exception):

                  using (var connection = new SqlConnection(ConnectionString))
                  {
                      var tasks = new List<Task>();
                      soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url))); 
                      rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));
                      await Task.WhenAll(tasks);
                  }
                  
                  ...
                  
                  private async Task InsertUrlAsync(IDbConnection connection, string url)
                  {
                      const string query = "INSERT INTO ..";
                      return await connection.ExecuteAsync(query, new { .. });
                  }
                  

                  the exception:

                  Message: System.InvalidOperationException : Invalid operation. The connection is closed.

                  but when I change the code to the following, it works:

                  var tasks = new List<Task>();
                  await connection.OpenAsync();
                  soldUrls.ForEach(....) .. etc ... 
                  
                  

                  Similar SO questions:

                  • Task.WhenAll, connection is closing
                  • SqlConnection closes unexpectedly inside using statement

                  解决方案

                  Dapper opens the connection for you; but it also closes it when work is done.

                  Now, you are running two independent async tasks using one single connection.

                  soldUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url))); 
                  rentUrls.ForEach(url => tasks.Add(InsertUrlAsync(connection, url)));
                  

                  Both the tasks are running simultaneously. When work of one task finish, it closes the connection. But other task is still running which does not have access to open connection anymore and hence the exception you mentioned in question.

                  As you said, if you open the connection yourself, Dapper does not close it and everything just works fine.

                  By the way, while using connection instance concurrently, there may be unexpected issues. Please refer to this question for more details.

                  这篇关于为什么我需要手动打开这个 .NET Core 数据库连接,而通常 Dapper 会自动打开它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:.NET 5 + Microsoft.Data.SqlClient - 从传输流中收到意外 下一篇:实体框架核心 5 - 从单个存储过程返回两个 orac

                  相关文章

                  最新文章

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

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

                      <tfoot id='lwwTb'></tfoot>

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