我正在尝试使用 context.ExecuteStoreCommand 对我的数据模型执行查询,但是我会收到错误消息:
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' ...我以前用过这个项目,我看不到我失踪了。我有项目引用EntityFramework,System.Data,System.Data.Entity。 MSDN文档说ObjectContext是System.Data.Object的一部分,但我不能直接引用,我不需要引用它以前使用它。我缺少什么?
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?
推荐答案回答我自己的问题:
我最初使用ExecuteStoreCommand的代码已经在一个EF4项目中,其中代码生成策略被设置为Default,这导致正在创建一个ObjectContext。阅读这里,VS2012上的EF5似乎默认为数据模型创建了一个DbContext。
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 - 通过更改代码生成策略,从DbContext更改为ObjectContext。 此问题显示了如何执行此操作。可以使用< object context> .ExecuteStoreCommand()。
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 - 执行SQL命令DbContext,例如:
2 - Execute the SQL command against the DbContext, for example:
< db context> .Database.SqlQuery(typeof(myTable),SELECT * FROM myTable code>
<db context>.Database.SqlQuery(typeof(myTable), "SELECT * FROM myTable")
更多推荐
上下文不包含ExecuteStoreCommand的定义
发布评论