如何在 EF Core 3.1 中使用脚手架流程更新我的实体 - 执行超时已过期

Posted

技术标签:

【中文标题】如何在 EF Core 3.1 中使用脚手架流程更新我的实体 - 执行超时已过期【英文标题】:How do I update my Entities using Scaffoling process in EFCore 3.1 -- Execution Timeout Expired 【发布时间】:2021-03-15 03:01:35 【问题描述】:

我能够运行我的 API 并进行从数据库获取数据的调用。然后当我尝试更新我的实体时 - 进行数据库更改后我收到错误:

Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

堆栈跟踪是:

PM> Scaffold-DbContext 'Server=connection-string;Persist Security Info=False;User ID=username;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;' Microsoft.EntityFrameworkCore.SqlServer -ContextDir Context -OutputDir Entities -Context HomeaZZonEntities -Force

Build started...
Build succeeded.

Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
 ---> System.ComponentModel.Win32Exception (258): The wait operation timed out.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TrySetMetaData(_SqlMetaDataSet metaData, Boolean moreInfo)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.GetColumns(DbConnection connection, IReadOnlyList`1 tables, String tableFilter, String viewFilter, IReadOnlyDictionary`2 typeAliases)
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.GetTables(DbConnection connection, Func`3 tableFilter, IReadOnlyDictionary`2 typeAliases)
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, DatabaseModelFactoryOptions options)
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, DatabaseModelFactoryOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:e9dbb919-9783-48c3-ae86-a6fda3d98427
Error Number:-2,State:0,Class:11
Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

我尝试了所有其他帖子中的所有内容,例如修改超时时间 - 但大约 15 秒后仍然失败。

不过,几个月前我上次更新时确实工作过。

【问题讨论】:

【参考方案1】:

运行 Scaffold-DbContext "Server=.\SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型

你也可以参考这个 https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

【讨论】:

正确,我必须使用本地数据库...但这一定是 Microsoft Azure 的一个错误,因为几周前我还能够从 Azure 数据库搭建脚手架...

以上是关于如何在 EF Core 3.1 中使用脚手架流程更新我的实体 - 执行超时已过期的主要内容,如果未能解决你的问题,请参考以下文章

将 EF Core 搭建到现有 DbContext 的脚手架

ASP.NET MVC Core/6:EF 6 脚手架错误

在 .NET CORE 3.1 应用程序中访问使用脚手架生成的视图时出错

EF Core 3.1 (LINQ) - 如何在多列上左加入和分组

如何使用 ef core 对具有原始表名和属性的数据库进行反向工程

EF Core - NodaTime 类型的脚手架?