问题
I'm trying to execute a query against my data model with context.ExecuteStoreCommand
but I keep getting the error message:
<object context> does not contain a definition for 'ExecuteStoreCommand' ...
I've used this before in an old project and I can't see what I'm missing. I've got project references to EntityFramework, System.Data, System.Data.Entity. The MSDN documentation says that ObjectContext is part of System.Data.Object but I can't reference that directly and I've not needed to reference it where I've used it before. What am I missing?
回答1:
To answer my own question:
The code where I'd originally used ExecuteStoreCommand had been on an EF4 project where the Code Generation Strategy was set to "Default" which leads to an ObjectContext being created. Reading here it seems that EF5 on VS2012 creates a DbContext for the data model by default.
I found two ways around this problem.
1 - Change from DbContext to ObjectContext by changing the code generation strategy. This question shows how to do it. <object context>.ExecuteStoreCommand()
can then be used.
2 - Execute the SQL command against the DbContext, for example:
<db context>.Database.SqlQuery(typeof(myTable), "SELECT * FROM myTable")
来源:https://stackoverflow.com/questions/15205692/context-does-not-contain-a-definition-for-executestorecommand