什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中

时间:2023-03-09
本文介绍了什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在针对 SQL Server 2008 数据库运行的 C# 程序中,从 SQL Server 视图中选择字段的特定 LINQ-to-SQL 查询在我的本地开发环境中运行良好,但在暂存环境中运行时产生异常:

A particular LINQ-to-SQL query selecting fields from a SQL Server view in a C# program running against a SQL Server 2008 database, which runs fine in my local dev environment, produces an exception when run in the staging environment:

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 ...] 

是什么导致了这个异常的发生?

What is causing this exception to occur?

推荐答案

这可能是由于 LINQ 查询试图选择目标数据库视图或表中实际不存在的字段引起的.

This can be caused by a LINQ query that's trying to select a field that doesn't actually exist on the target database view or table.

这种情况可能发生的一种方式(在我的情况下是问题)是忽略将最近创建的实体框架迁移部署到目标环境,该迁移将新字段添加到正在查询的视图中.

One way this can happen (which was the problem in my case) is neglecting to deploy to the target environment a recently-created Entity Framework migration that adds the new field to the view being queried.

另一件要注意的事情是抛出的 EntityCommandExecutionException 的内部异常(如错误消息所建议的那样).在这种情况下,内部异常的类型为 SqlException 并具有有用的消息 Invalid column name ‘[my column name]’.

Another thing to look at is the inner exception of the thrown EntityCommandExecutionException (as suggested by the error message). In this case, the inner exception was of type SqlException and had the helpful message Invalid column name ‘[my column name]’.

因此,当运行 LINQ-to-SQL 查询时抛出 EntityCommandDefinition.ExecuteStoreCommands 的 EntityCommandExecutionException 时要注意的事项:

So, things to look at when a EntityCommandExecutionException at EntityCommandDefinition.ExecuteStoreCommands gets thrown when running a LINQ-to-SQL query:

  • 检查内部异常(如外部异常的错误消息所建议的那样).
  • 确保所有实体框架迁移都已部署到目标环境(如果正在使用 EF).
  • 检查查询是否试图选择不存在的字段.

这篇关于什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:删除 LINQ to SQL 记录而不先加载它 下一篇:LINQ:获取表列名称

相关文章

最新文章