Context does not contain a definition for ExecuteStoreCommand

会有一股神秘感。 提交于 2019-12-12 10:20:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!