【问题标题】:What can cause an EntityCommandExecutionException in EntityCommandDefinition.ExecuteStoreCommands?什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?
【发布时间】:2015-06-11 15:24:32
【问题描述】:

在针对 SQL Server 2008 数据库运行的 C# 程序中,从 SQL Server 视图中选择字段的特定 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 ...] 

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

【问题讨论】:

    标签: c# sql-server linq-to-sql


    【解决方案1】:

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

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

    要查看的另一件事是引发的 EntityCommandExecutionException 的内部异常(如错误消息所示)。在这种情况下,内部异常属于 SqlException 类型,并具有有用的消息 Invalid column name ‘[my column name]’

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

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

    【讨论】:

      【解决方案2】:

      这可能是由于连接字符串中缺少"Multiple Active Result Sets"

      多个活动结果集 (MARS) 是一项允许在单个连接上执行多个批处理的功能。在以前的版本中,一次只能针对单个连接执行一个批处理。使用 MARS 执行多个批次并不意味着同时执行操作。

      修复:

      string connectionString = "Data Source=MSSQL1;" + 
      "Initial Catalog=AdventureWorks;Integrated Security=SSPI;" +
      "MultipleActiveResultSets=True";
      

      【讨论】:

        【解决方案3】:

        我帮助访问了类似的本地属性。例外:

        foreach (var myTableObject in context.Table)
        {
            // Exception
        }
        
        
        foreach (var myTableObject in context.Table.Local)
        {
            // No exception
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-17
          • 2020-02-03
          • 2013-02-25
          • 2012-12-31
          • 2014-01-01
          • 2017-03-10
          • 2019-12-01
          相关资源
          最近更新 更多