代码第一个自定义SQL迁移超时异常 [英] Code first custom SQL migration timeout exception

查看:219
本文介绍了代码第一个自定义SQL迁移超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的迁移类如下所示:


我正在尝试使用实体框架迁移创建FULL TEXT索引。

  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 [附件](
内容
TYPE COLUMN ContentType
语言'ENGLISH'

KEY INDEX [PK_dbo.Attachments]
ON FullTextIndexes;,true);
}

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

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

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



但是从迁移项目运行时,第二个Sql请求触发异常



< blockquote>

超时已过期。在完成操作或服务器之前经过的超时时间没有响应。


使用-Verbose标志的完整堆栈跟踪: p>

 更新数据库-ConnectionStringName DatabaseContext -Verbose 
使用StartUp项目'Lx2'。
使用NuGet项目'Database.Model'。
指定'-Verbose'标志来查看应用于目标数据库的SQL语句。
目标数据库是:'Lx2'(DataSource:。,Provider:System.Data.SqlClient,Origin:Explicit)。
应用显式迁移:[201406050348083_AttachmentsContentFullTextIndex]。
应用显式迁移:201406050348083_AttachmentsContentFullTextIndex。
ALTER TABLE [dbo]。[附件] ALTER COLUMN [ContentType] [nvarchar](260)NULL
CREATE FULLTEXT CATALOG FullTextIndexes AS DEFAULT;
CREATE FULLTEXT INDEX ON [附件](
内容
TYPE COLUMN ContentType
语言'ENGLISH'

KEY INDEX [PK_dbo.Attachments]
ON FullTextIndexes;
System.Data.SqlClient.SqlException(0x80131904):超时过期。在完成操作或服务器之前经过的超时时间没有响应。 ---> System.ComponentModel.Win32Exception(0x80004005):等待操作超时

在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)
在系统System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)中的
在System .Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady)
在System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,Boolean async,Int32 timeout,Boolean asyncWrite)
在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1完成,String methodName,Boolean sendT oPipe,Int32 timeout,Boolean asyncWrite)
在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher。< NonQuery> b__0(DbCommand t, DbCommandInterceptionContext`1 c)
在System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch [TTarget,TInterceptionContext,TResult](TTarget目标,Func`3操作,TInterceptionContext interceptionContext,Action`3执行,Action `3执行)
在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext interceptionContext)
在System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
在System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction事务,MigrationStatement migrationStatement,DbInterceptionContext interceptionContext)
在System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransac System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements,DbTransaction事务,DbInterceptionContext interceptionContext)中的
$ System.Data.Entity.Migrations中的
。 DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements,DbConnection连接)
在System.Data.Entity.Migrations.DbMigrator。  c__DisplayClass30。< ExecuteStatements> b__2e()
在System.Data.Entity在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func`1操作)
的System.DocuaultSqlExecutionStrategy。c__DisplayClass1。< Execute> b__0()
。 Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action操作)
在System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements,DbTransaction existingTransaction)
在Syste m.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
在System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
在System.Data。 Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId,XDocument targetModel,IEnumerable`1 operations,IEnumerable`1 systemOperations,Boolean downgrading,Boolean auto)
在System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
在System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration,DbMigration lastMigration)
在System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId,String lastMigrationId)
在System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)
在System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
在System.Data.Entity.Migrations.DbMigrator。  c__DisplayClassc。<更新> b__b()
在系统.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
在System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
在System.Data.Entity.Migrations.DbMigrator .Update(String targetMigration)
在System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
在System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)上的
$ System.AppDomain.DoCallBack中的
(CrossAppDomainDelegate callBackDelegate)
在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
在System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)
在System.Data.Entity.Migrations.UpdateDatabaseCommand。  c__DisplayClass2。< .ctor> b__0()
在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute (Action命令)
ClientConnectionId:3d298f0a-e2dc-4976-8587-c69d03b23c6b
超时过期。在完成操作或服务器之前经过的超时时间没有响应。

我试图从Verbose输出中全部复制所有SQL,并直接在Management Studio中执行它的工作原理与预期的没有错误。



任何想法如何修复?



奇怪的事情另外,如果我将ContentType最大长度更改代码

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

在单独的迁移文件中,一切也正常。



更新:



shivakumar 建议后,我尝试增加迁移配置中的连接超时时间(最多5分钟),并在接收到超时异常之前增加时间,但问题仍然存在。

解决方案>

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

 内部密封类配置:DbMigrationsConfiguration< ApplicationDbContext> 
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey =YourDbContext;

//新的超时(以秒为单位)
this.CommandTimeout = 60 * 5;
}
}


I am trying to create FULL TEXT index using Entity Framework Migration by executing custom Sql.

My migration class looks like this:

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");
    }
}

When I run it from MSSQL management studio everything is perfect and SQL did exactly what I am expected from it.

But when running from migration project second Sql request fires exception

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

Full stack trace with -Verbose flag:

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.

I tried to copy all SQL from Verbose output exactly 'as is' and execute it directly in Management Studio and it work exactly as expected with no errors.

Any ideas how this can be fixed?

Strange thing also that if I put ContentType max length changing code

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

in separate migration file everything also works fine.

UPDATE:

After shivakumar advice I tried to increase connection timeout (up to 5 minutes) in migration configuration and this increased time before I received "Timeout exception" but problem is still there.

解决方案

Use Configuration.cs file to set custom time out:

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

        // New timeout in seconds
        this.CommandTimeout = 60 * 5; 
    }
}

这篇关于代码第一个自定义SQL迁移超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆