gpt4 book ai didi

c# - 什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?

转载 作者:IT王子 更新时间:2023-10-29 04:33:53 31 4
gpt4 key购买 nike

在针对 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 时需要注意的事项:

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

关于c# - 什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785019/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com