上下文不包含ExecuteStoreCommand的定义 [英] Context does not contain a definition for ExecuteStoreCommand

查看:218
本文介绍了上下文不包含ExecuteStoreCommand的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆