C# EF 6 System.Data.Objects.ObjectContext 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14106286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EF 6 System.Data.Objects.ObjectContext Error
提问by Joe
I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following error:
我最近从实体框架 5 升级到实体框架 6 Alpha 2,但出现以下错误:
Method not found: 'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.
找不到方法:'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'。
This is getting hit when I call
当我打电话时,这受到了打击
if (Membership.ValidateUser(model.UserName, model.Password)) {}
This used to work fine before not sure why it's springing this error. Any suggestions?
在不确定为什么会出现此错误之前,这曾经可以正常工作。有什么建议?
采纳答案by Joe
EF 6 does not have System.Data.Objects.ObjectContext
. EF 6 has moved some types, including ObjectContext
, from System.Data.Entity.dll
into EntityFramework.dll
, and changed their namespaces. The fact that you get this error suggests you haven't attempted to recompile your application, you've simply replaced EntityFramework.dll
and hoped for the best. That won't work. You need to update your code to work with EF 6: you need to remove your references to System.Data.Entity.dll
, and update your code to refer to the new types.
EF 6 没有System.Data.Objects.ObjectContext
. EF 6 已将一些类型,包括ObjectContext
、 从 移动System.Data.Entity.dll
到EntityFramework.dll
并更改了它们的命名空间。您收到此错误的事实表明您没有尝试重新编译您的应用程序,您只是简单地替换EntityFramework.dll
并希望最好。那行不通。您需要更新您的代码以使用 EF 6:您需要删除对 的引用System.Data.Entity.dll
,并更新您的代码以引用新类型。
It just might be possible for the reference to the IObjectContextAdapter.ObjectContext
property to be in some library you're using, but most likely it'll be in your own code. The error message (in the part you didn't include in your question) should tell you where it is coming from.
对该IObjectContextAdapter.ObjectContext
属性的引用可能会出现在您正在使用的某个库中,但很可能会出现在您自己的代码中。错误消息(在您未包含在问题中的部分)应该告诉您它来自哪里。
回答by Leniel Maccaferri
I'm also using EF 6
.
我也在用EF 6
。
I managed to solve the problem uninstalling the package Microsoft.AspNet.Providers.Core
v. 1.2. I'm using version 1.1 instead. If you're like me and is using LocaDb
, you'll have to uninstall the LocaDb
package because it depends on that package. Of course you'll have to reinstall LocaDb again...
我设法解决了卸载Microsoft.AspNet.Providers.Core
1.2 版软件包的问题。我正在使用 1.1 版。如果您像我一样正在使用LocaDb
,则必须卸载该LocaDb
软件包,因为它取决于该软件包。当然,您必须再次重新安装 LocaDb...
You can grab v. 1.1 using the NuGet Package Manager Console inside Visual Studio:
您可以使用 Visual Studio 中的 NuGet 包管理器控制台获取 v. 1.1:
Install-Package Microsoft.AspNet.Providers.Core -Version 1.1
There's a Microsoft Connect bug filled regarding this issue:
有一个关于此问题的 Microsoft Connect 错误:
回答by Eraph
I managed to resolve this by removing the AspNet Providers I had installed through Nuget, which was marked as deprecated. Doing this also uninstalled Entity Framework.
我设法通过删除我通过 Nuget 安装的 AspNet Providers 来解决这个问题,它被标记为已弃用。这样做也卸载了实体框架。
I then installed the new AspNet Universal Providers, followed by Entity Framework 6, and all my problems were fixed.
然后我安装了新的 AspNet Universal Providers,接着是 Entity Framework 6,我的所有问题都得到了解决。
回答by Fabian Nicollier
The new 2.0 version of the providers (http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/) are EF6 compatible (they'll actually only work with EF6).
提供程序的新 2.0 版本 ( http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/) 与 EF6 兼容(它们实际上只适用于 EF6)。
回答by piris
For me updating these below worked:using System.Data.Objects; --> using System.Data.Entity.Core.Objects;
对我来说,更新以下这些有效:using System.Data.Objects; --> using System.Data.Entity.Core.Objects;
using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;
using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;
回答by TSN_Prasad
Check This Link
检查此链接
http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx
http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx
I Updated the EF 6.2 and get same error and find the solution as fallows
我更新了 EF 6.2 并得到相同的错误并找到了作为休耕的解决方案
Change the namespace System.Data.Entity to System.Data.Entity.Core, including any references to System.Data.* namespaces (for example, System.Data.Objects becomes System.Data.Entity.Core.Objects).
将命名空间 System.Data.Entity 更改为 System.Data.Entity.Core,包括对 System.Data.* 命名空间的任何引用(例如,System.Data.Objects 变为 System.Data.Entity.Core.Objects)。
回答by swaraj
That happens when entity framework is unable to find the method in the dotnet framework library installed in the machine. So install dotnet framework 4.5.2 or higher. It will fix the issue.
当实体框架无法在机器中安装的 dotnet 框架库中找到方法时,就会发生这种情况。所以安装 dotnet framework 4.5.2 或更高版本。它会解决这个问题。
回答by Anytoe
What worked for me was the following:
对我有用的是以下内容:
Install the dll 'Microsoft.AspNet.DataSource' with:
PM> Install-Package Microsoft.AspNet.EntityDataSource
Reference 'Microsoft.AspNet.DataSource.dll' in your project.
Added the following using declarations:
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using Microsoft.AspNet.EntityDataSource;
Removed the following using declarations:
using System.Data.Entity;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Web.UI.WebControls;
使用以下命令安装 dll 'Microsoft.AspNet.DataSource':
PM> 安装包 Microsoft.AspNet.EntityDataSource
在您的项目中引用“Microsoft.AspNet.DataSource.dll”。
添加了以下 using 声明:
使用 System.Data.Entity.Core.Metadata.Edm;
使用 System.Data.Entity.Core.Objects;
使用 Microsoft.AspNet.EntityDataSource;
删除了以下 using 声明:
使用 System.Data.Entity;
使用 System.Data.Metadata.Edm;
使用 System.Data.Objects;
使用 System.Web.UI.WebControls;
Voila, code is compiling and working.
瞧,代码正在编译和工作。
回答by Anthony Mason
A quick and simple fix for me was to remove the offending assemblies (deprecated) and added a reference to the new library. The code was modified within the Context.tt and looks like this:
对我来说,一个快速而简单的修复方法是删除有问题的程序集(已弃用)并添加对新库的引用。在 Context.tt 中修改了代码,如下所示:
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects; // The assembly you need
using System.Linq;
<#
}
Before any modifications it had appeared as such:
在任何修改之前,它是这样出现的:
if (container.FunctionImports.Any())
{
#>
using System.Data.Objects; // Error on compile
using System.Data.Objects.DataClasses; // Error on compile
using System.Linq;
<#
回答by Cesar Alvarado Diaz
It has an old version associated with edmx file for this:
它有一个与 edmx 文件相关的旧版本:
- Reinstall EF with Nuget
- Delete the .edmx file and recreate it with tables
- 使用 Nuget 重新安装 EF
- 删除 .edmx 文件并使用表重新创建它