[英]EF, SQL Server - Connection Timeout Expired. The timeout period elapsed during the post-login phase
I've created an ASP.NET MVC project that uses Entity Framework and Identity.我创建了一个使用实体框架和标识的 ASP.NET MVC 项目。
The problem is that when I debug my project, I get an error.问题是当我调试我的项目时,出现错误。
(note: If I delete my DB from SQL Server Object Explorer and run it again this error don't show up, but I don't want to manually delete it every time) (注意:如果我从 SQL Server 对象资源管理器中删除我的数据库并再次运行它,则不会出现此错误,但我不想每次都手动删除它)
Error:错误:
Connection Timeout Expired.
连接超时已过期。 The timeout period elapsed during the post-login phase.
在登录后阶段超时时间已过。 The connection could have timed out while waiting for server to complete the login process and respond;
连接可能在等待服务器完成登录过程并响应时超时; Or it could have timed out while attempting to create multiple active connections.
或者它可能在尝试创建多个活动连接时超时。 The duration spent while attempting to connect to this server was - [Pre-Login] initialization=15;
尝试连接到此服务器所花费的持续时间为 - [Pre-Login] 初始化 = 15; handshake=3;
握手=3; [Login] initialization=0;
【登录】初始化=0; authentication=2;
认证=2; [Post-Login] complete=14991
[登录后]完成=14991
Connection string:连接字符串:
<add name="MovieReviewsDb"
providerName="System.Data.SqlClient"
connectionString="Data Source(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\MovieReviewsDb.mdf;Initial Catalog=MovieReviewsDb;Integrated Security=SSPI;MultipleActiveResultSets=True" />
I've set custom initializer that inherits from DropCreateDatabaseAlways
.我已经设置了从
DropCreateDatabaseAlways
继承的自定义初始值设定DropCreateDatabaseAlways
。 I have overridden the Seed
method where I populate the database with 3 records and creates default roles and users (Identity).我已经覆盖了用 3 条记录填充数据库并创建默认角色和用户(身份)的
Seed
方法。
(note: my DbContext
inherits from IdentityDbContext
) (注意:我的
DbContext
继承自IdentityDbContext
)
I'm stuck at this for several hours and searched through a lot of similar posts ( like this one ) but I can't find an answer.我坚持了几个小时并搜索了很多类似的帖子( 比如这个),但我找不到答案。
Is it possible to set a bigger timeout (maybe It takes too long and the timeout is reached) and how?是否可以设置更大的超时时间(可能需要太长时间并且达到超时时间)以及如何设置?
I think I've finally figured it out. 我想我终于明白了。
I've added ;Connection Timeout=120;
我添加了
;Connection Timeout=120;
to my connection string and it works. 到我的连接字符串,它的工作原理。 I guess it just needed more time.
我想这只需要更多时间。
There are two different solutions increase the timeout or retry policy for network problems有两种不同的解决方案增加网络问题的超时或重试策略
Change the connection string as below.更改连接字符串如下。
"ConnectionString": "...;Connection Timeout=120;"
Refactoring code in manner so that it would allow you to call the query recursively till you get a desired result.以某种方式重构代码,以便它允许您递归地调用查询,直到获得所需的结果。
for example Polly retry policy例如Polly重试策略
Re-try to open a SQL Server connection if failed 如果失败,请重新尝试打开 SQL Server 连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.