在针对 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:
这篇关于什么会导致 EntityCommandDefinition.ExecuteStoreCommands 中的 EntityCommandExecutionException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
LINQ to SQL:如何在不检索整个实体的情况下更新唯LINQ to SQL: how to update the only field without retrieving whole entity(LINQ to SQL:如何在不检索整个实体的情况下更新唯一字段)
string1 >= string2 未在 Linq to SQL 中实现,有什string1 gt;= string2 not implemented in Linq to SQL, any workaround?(string1 gt;= string2 未在 Linq to SQL 中实现,有什么解决方法吗?)
如何在条件下使用 RemoveAll 删除列表中的多个项目How to Remove multiple items in List using RemoveAll on condition?(如何在条件下使用 RemoveAll 删除列表中的多个项目?)
转换表达式树Convert Expression trees(转换表达式树)
当 IDENTITY_INSERT 设置为 OFF 时,无法为表“ClientCannot insert explicit value for identity column in table #39;ClientDetails#39; when IDENTITY_INSERT is set to OFF(当 IDENTITY_INSERT 设置为 OFF 时,
Linq 独特的 &最大限度Linq distinct amp; max(Linq 独特的 amp;最大限度)