What can cause an EntityCommandExecutionException in EntityCommandDefinition.ExecuteStoreCommands?

前端 未结 3 886
自闭症患者
自闭症患者 2020-12-09 15:59

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 environmen

相关标签:
3条回答
  • 2020-12-09 16:19

    Me helped the access to the Local property like. The exception :

    foreach (var myTableObject in context.Table)
    {
        // Exception
    }
    
    
    foreach (var myTableObject in context.Table.Local)
    {
        // No exception
    }
    
    0 讨论(0)
  • 2020-12-09 16:26

    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.

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

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

    • Examine the inner exception (as suggested by the outer exception’s error message).
    • Make sure all Entity Framework migrations have been deployed to the target environment (if EF is in use).
    • Check and see whether the query is trying to select a field that doesn’t exist.
    0 讨论(0)
  • 2020-12-09 16:28

    This can be caused by "Multiple Active Result Sets" missing in connection String.

    Multiple Active Result Sets (MARS) is a feature that allows the execution of multiple batches on a single connection. In previous versions, only one batch could be executed at a time against a single connection. Executing multiple batches with MARS does not imply simultaneous execution of operations.

    To Fix:

    string connectionString = "Data Source=MSSQL1;" + 
    "Initial Catalog=AdventureWorks;Integrated Security=SSPI;" +
    "MultipleActiveResultSets=True";
    
    0 讨论(0)
提交回复
热议问题