首先代码自定义SQL迁移超时异常

2024-01-11

我正在尝试通过执行自定义 Sql 使用实体框架迁移创建全文索引。

我的迁移类如下所示:

public partial class DocumentContentFullTextIndex : DbMigration
{
    public override void Up()
    {
        AlterColumn("dbo.Attachments", "ContentType", c => c.String(maxLength: 260));

        Sql("CREATE FULLTEXT CATALOG FullTextIndexes AS DEFAULT;", true);

        Sql(@"CREATE FULLTEXT INDEX ON [Attachments](
Content
    TYPE COLUMN ContentType
    Language 'ENGLISH'
)
KEY INDEX [PK_dbo.Attachments]
ON FullTextIndexes;", true);
    }

    public override void Down()
    {
        AlterColumn("dbo.Attachments", "ContentType", c => c.String(maxLength: null));

        Sql("DROP FULLTEXT INDEX ON [Attachments]");
        Sql("DROP FULLTEXT CATALOG FullTextIndexes");
    }
}

当我从 MSSQL 管理工作室运行它时,一切都很完美,并且 SQL 完全符合我的预期。

但是当从迁移项目运行第二个 Sql 请求时会引发异常

超时已过。操作完成之前超时时间已过,或者服务器未响应。

带有 -Verbose 标志的完整堆栈跟踪:

Update-Database -ConnectionStringName DatabaseContext -Verbose
Using StartUp project 'Lx2'.
Using NuGet project 'Database.Model'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'Lx2' (DataSource: ., Provider: System.Data.SqlClient, Origin: Explicit).
Applying explicit migrations: [201406050348083_AttachmentsContentFullTextIndex].
Applying explicit migration: 201406050348083_AttachmentsContentFullTextIndex.
ALTER TABLE [dbo].[Attachments] ALTER COLUMN [ContentType] [nvarchar](260) NULL
CREATE FULLTEXT CATALOG FullTextIndexes AS DEFAULT;
CREATE FULLTEXT INDEX ON [Attachments](
Content
    TYPE COLUMN ContentType
    Language 'ENGLISH'
)
KEY INDEX [PK_dbo.Attachments]
ON FullTextIndexes;
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:3d298f0a-e2dc-4976-8587-c69d03b23c6b
Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

我尝试“按原样”从 Verbose 输出中复制所有 SQL,并直接在 Management Studio 中执行它,它完全按照预期工作,没有错误。

有什么想法可以解决这个问题吗?

奇怪的是,如果我把 ContentType 最大长度更改代码

AlterColumn("dbo.Attachments", "ContentType", c => c.String(maxLength: 260));

在单独的迁移文件中,一切都工作正常。

UPDATE:

After 希瓦库马尔 https://stackoverflow.com/users/2248845/shivakumar我尝试在迁移配置中增加连接超时(最多 5 分钟),并且在收到“超时异常”之前增加了时间,但问题仍然存在。


使用 Configuration.cs 文件设置自定义超时:

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "YourDbContext";

        // New timeout in seconds
        this.CommandTimeout = 60 * 5; 
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

首先代码自定义SQL迁移超时异常 的相关文章

  • 具有通用存储库、依赖项注入和 SoC 的 EF6 Code First

    经过大量阅读和尝试之后Entity Framework最新稳定版本 6 1 1 我读到了很多关于是否使用存储库的矛盾EF6 or EF一般来说 因为它是DbContext已经提供了一个存储库并且DbSet the UoW 盒子外面 让我首先
  • 根据聚合创建大小均匀的组

    可能是一个新手问题 但我希望根据数据库总大小将我们的服务器库存分成几个大小均匀的组 并且很难弄清楚如何对它们进行分组 我认为 NTILE 可能会起作用 但我就是无法将注意力集中在平均分配组上 我下面的示例只是随机订购服务器 我希望结果是大小
  • 实体框架是否支持 SQLClient 连接字符串中的 ApplicationIntent=ReadOnly

    实体框架是否支持应用程序意图 只读 http msdn microsoft com en us library hh205662 aspx在 SQLClient 连接字符串中 它是参考对可用性副本的只读访问 http msdn micros
  • 部署在 Azure 中时在 EF 迁移中使用更新数据库

    上下文 我在 Azure 中部署了 ASP NET MVC4 解决方案 我的 MSSQL Server 数据库也在 Azure 中 我目前的部署方式是这样的 在 web config 中 我将连接字符串从本地数据库 sdf 更改为 azur
  • 使用表达式动态决定打开哪个子报表

    我有一份报告 其中包含几页带有图表 表格等的内容 该报告由总是希望对报告进行小修改的不同客户使用 目前 如果客户想要对第 5 页进行更改 则会通过修改重新创建整个报告 即使唯一的更改是在第 5 页的图表中 更重要的是 一些客户不想看到第 3
  • ASP.NET MVC 4,迁移 - 如何在生产服务器上运行“更新数据库”

    我可以使用包管理器在本地运行 update database verbose 可能是一个愚蠢的问题 但我无法在线找到它 一旦我的网站部署 我如何在服务器上手动运行它 其次 您会推荐哪些其他策略来将数据库迁移部署到生产环境 以及它们如何更好
  • SQL分组和总结

    我的表如下所示 income date productid invoiceid customerid 300 2015 01 01 A 1234551 1 300 2016 01 02 A 1234552 1 300 2016 01 03
  • EF - 从自动迁移转向手动迁移

    结束了漫长的一天测试各种场景 我不必重新创建生产数据库 我们从 EF 开始 在开发过程中没有足够明智地从自动迁移转向命名迁移 现在 我正在尝试倒带时钟 并创建与生产数据库一致的初始迁移 是否可以将模型与迁移表中的自动迁移进行对齐 我应该创建
  • 实体框架:无效的列名称 *_ID 1

    我正在尝试为几个名为 的表实现 DbContextEmployee and Department 员工和部门之间的关系是多对一的 即部门可以有很多员工 下面是我设计的EntityFramework类 CodeFirst方法 Table Em
  • SQL Server Like 查询不区分大小写

    Query SELECT from Table 2 WHERE name like Joe Output 1 100 Joe 2 200 JOE 3 300 jOE 4 400 joe 为什么不区分大小写 Problem 查询不区分大小写
  • 使用 IdentityDbContext 和 Code First 自动迁移表位置和架构的实体框架?

    我正在尝试使用 IdentityDbContext 类设置自动迁移更新 并将更改传播到整个数据库的实际 DbContext 在进入代码之前 在使用自动迁移实现 IdentityDbContext 时 我收到此错误 影响迁移历史系统表位置的自
  • 实体框架在种子数据库上插入重复项[重复]

    这个问题在这里已经有答案了 EDIT From here https stackoverflow com questions 22581889 seeding database entity framework 6我尝试在播种方法中分配 I
  • 如何使用自定义 DbFunction 将字符串转换为小数

    我有一个将小数 和其他类型 保存为字符串的表 我想在数据库上下文上编写一个 Linq 查询 该查询将转换为数据库转换而不是本地转换 出于性能原因 This 非工作的example 从概念上讲是我想要实现的目标 using var conte
  • 为什么没有主键的表是一个坏主意?

    我对数据建模非常陌生 根据微软的实体框架 不允许使用没有主键的表 这显然是一个坏主意 我试图找出为什么这是一个坏主意 以及如何修复我的模型 这样我就不会出现这个漏洞 我当前的模型中有 4 个表 User City HelloCity 和 R
  • 指数数组的边界之外。 (微软.SqlServer.smo)

    我在用SQL Server 2008 R2 它运行良好 但最近 我改变了我的托管服务器 我发现他们已经安装了SQL Server 2012在服务器上 现在 问题是通过连接服务器数据库后SQL Server 2008 R2 当我单击任何表名称
  • 需要在SQL Server 2012中自动递增字符串

    考虑 SQL Server 2012 中的表 789 0000000 上面的数字在 SQL Server 2012 中将被视为字符串 但每当我更新记录时 我都需要增加到 1 例如 当我更新记录 1 时 它应该增加到789 0000001 当
  • 将语句插入 SQL Server 数据库

    最近几天我试图找到这个错误 但没有成功 我正在尝试在数据库中插入一个新行 一切都很顺利 没有错误 也没有程序崩溃 My INSERT声明如下 INSERT INTO Polozaj Znesek Uporabnik Cas Kupec Po
  • 如何使用 Entity Framework 和 Moq 进行单元测试?

    我是 Moq 新手 希望将其用作数据后备存储 但不接触实时数据库 我的设置如下 UnitOfWork 包含所有存储库 并用于整个应用程序中的数据访问 Repository 代表 DbSet 的直接挂钩 由 DbContext 提供 DbCo
  • 如何使用 EF Code First 将一张表与多个父项关联

    我正在构建一个域模型 该模型需要多个表才能被多个可能的父表引用 像您可能有一个表来存储注释或文件 并且这些注释和 或文件可以与不同的父实体关联 并不是说同一个 文件 或 注释 可以与多个所有者相关联 而是 文件 表中的 10 行中 其中 3
  • SQL Server 2008 R2 的 Try_Convert

    我正在使用 SQL Server 2008 R2 并且有一个VARCHAR我想要转换为的列DECIMAL 28 10 using CONVERT 但其中许多行的格式错误 因此无法将它们解析为数字 在这种情况下 我只想通过将结果设置为 0 或

随机推荐