首先代码自定义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迁移超时异常 的相关文章

  • 根据聚合创建大小均匀的组

    可能是一个新手问题 但我希望根据数据库总大小将我们的服务器库存分成几个大小均匀的组 并且很难弄清楚如何对它们进行分组 我认为 NTILE 可能会起作用 但我就是无法将注意力集中在平均分配组上 我下面的示例只是随机订购服务器 我希望结果是大小
  • EF 6 基于代码的迁移:向现有实体添加非空属性

    我想向现有表添加一个非空外键列 环境 EF 6 代码优先 基于代码的迁移 Code from Migration class for new entity Currency CreateTable dbo Currency c gt new
  • SQL分组和总结

    我的表如下所示 income date productid invoiceid customerid 300 2015 01 01 A 1234551 1 300 2016 01 02 A 1234552 1 300 2016 01 03
  • 实体框架:无效的列名称 *_ID 1

    我正在尝试为几个名为 的表实现 DbContextEmployee and Department 员工和部门之间的关系是多对一的 即部门可以有很多员工 下面是我设计的EntityFramework类 CodeFirst方法 Table Em
  • EF数据库首先如何针对数据库更改更新模型?

    在班级图书馆Ado net Entity Data Model已生成 POCO 类 这些是第一次生成的 但数据库更改并未得到反映 在edmx图表右键单击并选择Update Model from Database显示新创建的表 但即使选择添加
  • 如何连续添加起始行和下一行的值

    我只想创建一个 sql 查询 结果就像图片上的那样 类似于 SQL 中的斐波那契数列 Ex Column 1 10 则 Result 列的值为 Result 10 因为这是第一行 然后假设column1第二行的值为50 那么Result第二
  • 使用 EF6 连接到 SQL Server

    在 EF5 之前 为了连接到 SQL Server 2012 我所需要做的就是指定一个如下所示的连接字符串 Data Source SqlExpress Initial Catalog MyDatabase Integrated secur
  • 实体框架在种子数据库上插入重复项[重复]

    这个问题在这里已经有答案了 EDIT From here https stackoverflow com questions 22581889 seeding database entity framework 6我尝试在播种方法中分配 I
  • 任何 ToList() 的 EF6 NullReferenceException

    使用 C NET 4 5 2 Entity Framework 6 1 3 和 System Linq 我遇到了一个令人困惑的异常 异常本身似乎不包含有用的信息来确定引发该异常的原因 执行以下代码行会导致 NullReferenceExce
  • INNER/RIGHT/LEFT JOIN 怎么会比 FULL JOIN 慢 14 倍?

    我有一个查询 使用 FULL JOIN 需要 2 5 秒 使用 INNER RIGHT 或 LEFT JOIN 需要 40 秒 这是查询 子查询 完成两次 本身只需要 1 3 秒 SELECT T1 time T1 Total T1 rn
  • 从 SQL Server 2012 查询结果中减去小时数

    我正在 SQL Server 2012 Management Studio 中的警报系统信号自动化平台数据库上运行查询 但遇到了一些问题 我的查询运行得很好 但我无法将结果细化到我想要的水平 我正在选择一些格式为的列DATETIME 我只想
  • 如何检查DBContext是否已释放?

    我想与从外部 继承类 调用的另一个方法共享数据库上下文 而不创建新的上下文 除非正在释放它 我想检查上下文是否已处理 以便我可以创建新的上下文 这是休息 api 有多个实体的批量上传 我想共享事务 因此如果一个实体失败 它将不会提交到数据库
  • 将数据库优先迁移到代码优先 - 从发布切换到迁移

    我们需要将现有项目从数据库优先迁移到代码优先 每个环境 DEV TEST PROD 的数据库版本略有不同 差异在于 DEV 中的更改可能尚未推送到 TEST 和 PROD 我们使用实体框架 6 1 3 我们有一个数据库项目部署数据库 发布操
  • 计算树中值的总和(递归查询)

    我在表员工 id name parentid 中有树结构 并且该表可以嵌套 employees 与另一个具有列 id employeeid quantity 的 Sales 表是一对多关系 每个员工都有销售数量 我想计算每个员工以及儿童员工
  • 需要在SQL Server 2012中自动递增字符串

    考虑 SQL Server 2012 中的表 789 0000000 上面的数字在 SQL Server 2012 中将被视为字符串 但每当我更新记录时 我都需要增加到 1 例如 当我更新记录 1 时 它应该增加到789 0000001 当
  • 在存储过程中使用动态sql中的临时表

    我正在 SQL Server 2012 中编写存储过程 我有一个像这样定义的临时表 DECLARE CURRENT RET WEEK PTIMEIDS TABLE PTIMEID INT 我也在使用EXECUTE编写动态 SQL 查询 有什
  • 在 SQL Server 中将行转换为 XML 格式

    我有如下要求 上图的 ddl 和 dml 脚本是 CREATE TABLE example CCP DETAILS SID int ACCOUNT GROWTH int PRODUCT GROWTH int PROJECTION SALES
  • 实体类型 ApplicationUser 不是当前上下文模型的一部分。在项目开始时使用了两个不同的数据库

    我使用实体框架创建了一个 MVC 4 应用程序来读取数据并将数据写入我在 Azure 数据库上托管的数据库 Azure 数据库应该保存应用程序数据和应用程序的登录数据 但是 当我第一次创建应用程序时 我忘记删除到本地计算机的连接字符串 因此
  • 查询所有表数据并进行索引压缩

    是否有人碰巧拥有一个通用 SQL 语句 可以列出数据库中每个分区的所有表和索引及其当前的压缩设置 Thanks 编辑 这是我尝试查询表所得到的 但我不确定连接是否正确 我得到了重复项 这似乎是由于索引的存在引起的 SELECT t name
  • WinForms 中的 EF:如何过滤 BindingSource/DGW 中的数据 (.Local.ToBindingList())

    我按照本教程生成了 EF 模型 首先是数据库 和数据源http msdn microsoft com en us data jj682076 aspx http msdn microsoft com en us data jj682076

随机推荐