C# 连续连接数据库时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29840282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error when connect database continuously
提问by user3928324
When I am querying from database in continuous looping, after some time I get an error :
当我在连续循环中从数据库查询时,一段时间后出现错误:
An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy.
已引发异常,可能是由于暂时故障。如果要连接到 SQL Azure 数据库,请考虑使用 SqlAzureExecutionStrategy。
Normally it is working fine.
通常它工作正常。
回答by Jan Engelsberg
When connecting to SQL Database you have to account for transient connection failures. These connection failures can happen for example when updates are rolled out, hardware fails etc. The error you see indicates that one of these things happened which is way you connection was dropped. Enabling a Execution Strategy as suggested by Anbuj should solve the issue.
连接到 SQL 数据库时,您必须考虑暂时的连接故障。例如,当更新推出、硬件出现故障等时,可能会发生这些连接故障。您看到的错误表明发生了这些事情之一,即您的连接被断开。按照 Anbuj 的建议启用执行策略应该可以解决问题。
回答by Shantanu
Enable an execution strategy as mentioned here : https://msdn.microsoft.com/en-us/data/dn456835.aspx. When designing for Azure SQL DB, you have to design for transient connection failures, since back-end updates, hardware failures, load balancing can cause intermittent failures at times.
启用此处提到的执行策略:https: //msdn.microsoft.com/en-us/data/dn456835.aspx。在为 Azure SQL DB 进行设计时,必须针对瞬时连接故障进行设计,因为后端更新、硬件故障、负载平衡有时会导致间歇性故障。
回答by MaGnumX
If your database is local to let's say a WebAPI, sometimes you must provide data source=localhost not an IP address. We have the situation where we are using some VPS and without setting data source to a localhost gives this error. So, if anybody else has experienced this, it could help him.
如果您的数据库是本地的,比如 WebAPI,则有时您必须提供 data source=localhost 而不是 IP 地址。我们有这样的情况,我们正在使用一些 VPS 并且没有将数据源设置为本地主机会出现此错误。所以,如果其他人经历过这种情况,它可以帮助他。
回答by Rush Frisby
I get this error when the login I am trying to connect to the database with does not have an associated user in the database.
当我尝试连接到数据库的登录名在数据库中没有关联用户时,我收到此错误。
回答by Taran
This could be because of TLS setting, .net 4.5 framework don't support tls 1.2 by default and new SQL db is not compatible with older tls ver . so either disable tls 1.0,1.1 in your machine or update to .net 4.6.2
这可能是因为 TLS 设置,.net 4.5 框架默认不支持 tls 1.2 并且新的 SQL db 与旧的 tls ver 不兼容。所以要么在你的机器中禁用 tls 1.0,1.1 要么更新到 .net 4.6.2
回答by Shekhar
If you are using EF Core configure retry on failure for resilient connections :
如果您使用的是 EF Core,请为弹性连接配置失败时重试:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("your_connection_string", builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
base.OnConfiguring(optionsBuilder);
}
回答by Joshua George
The solution for this problem is through the suggested answer, use SetExecutionStrategy()to turn on a retry policy. Also make sure to derive from the class DbConfigurationso that Entity Framework can execute the method automatically.
此问题的解决方案是通过建议的答案,用于SetExecutionStrategy()打开重试策略。还要确保从类派生,DbConfiguration以便实体框架可以自动执行该方法。
You also want to make sure that your connection resiliency really works through setting up an command interception which creates connection errors so that you can confirm it works.
您还希望通过设置创建连接错误的命令拦截来确保您的连接弹性确实有效,以便您可以确认它是否有效。
回答by Trilok Pathak
I am posting this answer as I face lot of issue while researching the answer of the issue. Below is the detailed error message I was getting:
我发布这个答案是因为我在研究问题的答案时遇到了很多问题。以下是我收到的详细错误消息:
Dividing the errors in the parts as the error was too long:
由于错误太长,将错误划分为部分:
System.Data.Entity.Core.EntityException: *An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy. *--->
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Resource ID : 1. The request limit for the database is 30 and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
System.Data.Entity.Core.EntityException:*可能由于暂时故障而引发的异常。如果要连接到 SQL Azure 数据库,请考虑使用 SqlAzureExecutionStrategy。*--->
System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参阅内部异常。---> System.Data.SqlClient.SqlException:资源 ID : 1. 数据库的请求限制为 30 并且已达到。请参阅“ http://go.microsoft.com/fwlink/?LinkId=267637”以获得帮助。在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,Boolean breakConnection,Action`1 wrapCloseInAction)
After research I found that it was related to the limits of Azure SQL Database maximum logins. I was using 'Basic' service tire and max concurrent users can login with that is 30.
经过研究,我发现这与 Azure SQL 数据库最大登录数的限制有关。我使用的是“基本”服务轮胎,最大并发用户数可以登录为 30。
Azure has pricing tiers that have quite dramatic differences in performance. To achieve that, they throttle a lot of performance metrics, e.g. CPU power, requests per minute, etc.
Azure 的定价层在性能上有很大差异。为了实现这一点,他们限制了许多性能指标,例如 CPU 功率、每分钟请求数等。
This means that if you're pushing over your tier, your requests will start getting queued up as the CPU power / volume of requests is too high to process. This results in timeouts and then the request limit grows as requests wait to be processed. Eventually, it gets to the point where the database essentially goes down.
这意味着,如果您正在推动层级,您的请求将开始排队,因为 CPU 功率/请求量太高而无法处理。这会导致超时,然后随着请求等待处理,请求限制会增加。最终,它达到了数据库本质上崩溃的地步。
My experience is that the lower database levels, such as S0 and S1, are under-powered and shouldn't be used for anything other than development or very basic sites.
我的经验是,较低的数据库级别,例如 S0 和 S1,功能不足,不应该用于开发或非常基本的站点以外的任何其他内容。
There are some great tools in the Azure portal that allow you to debug what is going on with your database, such as the CPU graphs, index advisor and query performance insights.
Azure 门户中有一些很棒的工具可以让你调试数据库的运行情况,例如 CPU 图表、索引顾问和查询性能洞察。
Here are the related links:
以下是相关链接:
Conclusion:
结论:
Part 1:Enable an execution strategy as mentioned here: https://msdn.microsoft.com/en-us/data/dn456835.aspx.
Part 2: You need to upgrade the subscription in the Azure(if price permits).
第 1 部分:启用此处提到的执行策略:https: //msdn.microsoft.com/en-us/data/dn456835.aspx。
第 2 部分:您需要升级 Azure 中的订阅(如果价格允许)。
Thanks.
谢谢。
回答by omarmallat
I could see that nobody put the solution in case of Entity Framework, and not EF core. The easiest way to implement SqlAzureExecutionStrategy is:
我可以看到没有人把解决方案放在实体框架的情况下,而不是 EF 核心。实现 SqlAzureExecutionStrategy 的最简单方法是:
Go to Context.cs file that contains:
public partial class YourEntity : DbContextadd the reference:
using System.Data.Entity.SqlServer;add another class at the end of the file containing the following code:
public MyConfiguration() { SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); SetDefaultConnectionFactory(new LocalDbConnectionFactory("mssqllocaldb")); }
转到包含以下内容的 Context.cs 文件:
public partial class YourEntity : DbContext添加参考:
using System.Data.Entity.SqlServer;在包含以下代码的文件末尾添加另一个类:
public MyConfiguration() { SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); SetDefaultConnectionFactory(new LocalDbConnectionFactory("mssqllocaldb")); }
You may refer to this documentationfor more information.
您可以参考此文档以获取更多信息。

