实体框架6中的CreatedOn列

时间:2013-10-28 23:02:18

标签: c# entity-framework azure-sql-database

升级到Entity Framework 6后,我们实现了自己的 DbExecutionStrategy 。除了现有的 SqlAzureExecutionStrategy ,我们的策略还会记录异常。 事实证明,每15-30分钟实体框架抛出内部SqlException System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'CreatedOn'. 这是一个内部错误。如果某些表上存在CreatedOn列,EF似乎会进行一些定期检查。是否有任何优雅的方法来防止抛出此异常?

这是一个调用堆栈:

   at System.Data.SqlClient.SqlConnection.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, ref Boolean dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, ref Task task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, ref Task task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

2 个答案:

答案 0 :(得分:39)

过去的实体框架曾经在__MigrationHistory表中有一个“CreatenOn”列。

每次AppDomain启动时,都会检查数据库是否需要迁移。 EF实际上会尝试读取“Cr​​eatedOn”列,但显然会因记录的异常而失败。 EF在此检查周围有一个丑陋的try / catch所有块,如果抛出异常(列缺失),那么它不会尝试“迁移”CreatedOn列。

目前无法禁用该检查,只是不记录它...

答案 1 :(得分:3)

在我的情况下,发生此错误是因为我更改了 Visual Studio调试异常设置以中断所有异常(或者除了默认配置之外的更多例外)。重置Visual Studio的所有设置后,错误不再发生,我的应用程序按预期正常运行。

问题是Entity Framework有一个try / catch块来处理这个错误,以便在发生此错误时应用程序不会停止工作。处理完错误后,它会将应用程序返回到正常状态,就像在您自己的应用程序的try / catch块中一样。因此,打破这些异常会使我的代码不必要地停止

在我调试复杂程序时,必须打破所有异常,但是在我不再需要它之后我应该已经重置了调试异常设置。希望这可以帮助其他人经历同样困难的环境问题。

相关问题