在针对SQL Server 2008数据库运行的C#程序中,从SQL Server View 中选择字段的特定LINQ-to-SQL查询在我的本地开发环境中运行良好,在临时环境中运行时会产生异常:
Exception Message: An error occurred while executing the command definition. See the inner exception for details.
Exception Trace: System.Data.Entity.Core.EntityCommandExecutionException
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.b__5()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1..GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at [my code ...]
是什么导致此异常发生?
最佳答案
这可能是由于LINQ查询试图选择目标数据库 View 或表上实际上不存在的字段所致。
发生这种情况的一种方法(在我的情况下是问题)是忽略了将最近创建的Entity Framework迁移部署到目标环境,该迁移将新字段添加到要查询的 View 中。
另一件事要看的是抛出的EntityCommandExecutionException的内部异常(如错误消息所建议)。在这种情况下,内部异常的类型为SqlException,并且具有有用的消息Invalid column name ‘[my column name]’
。
因此,在运行LINQ-to-SQL查询时抛出EntityCommandDefinition.ExecuteStoreCommands处的EntityCommandExecutionException时要看的东西:
关于c# - 什么会导致EntityCommandDefinition.ExecuteStoreCommands中的EntityCommandExecutionException?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30785019/