简体   繁体   中英

EF 6 System.Data.Objects.ObjectContext Error

I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following error:

Method not found: '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?

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.

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.

对我来说,更新以下内容有效:
using System.Data.Objects; --> using System.Data.Entity.Core.Objects;

using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;

I'm also using 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...

You can grab v. 1.1 using the NuGet Package Manager Console inside Visual Studio:

Install-Package Microsoft.AspNet.Providers.Core -Version 1.1

There's a Microsoft Connect bug filled regarding this issue:

Microsoft.AspNet.Providers.Core incompatible with EF6

新的2.0版本的提供程序( http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/ )与EF6兼容(它们实际上仅适用于EF6)。

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.

I then installed the new AspNet Universal Providers, followed by Entity Framework 6, and all my problems were fixed.

Check This Link

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

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).

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.

What worked for me was the following:

  1. Install the dll 'Microsoft.AspNet.DataSource' with:

    PM> Install-Package Microsoft.AspNet.EntityDataSource

  2. Reference 'Microsoft.AspNet.DataSource.dll' in your project.

  3. Added the following using declarations:

    using System.Data.Entity.Core.Metadata.Edm;

    using System.Data.Entity.Core.Objects;

    using Microsoft.AspNet.EntityDataSource;

  4. Removed the following using declarations:

    using System.Data.Entity;

    using System.Data.Metadata.Edm;

    using System.Data.Objects;

    using System.Web.UI.WebControls;

Voila, code is compiling and working.

It has an old version associated with edmx file for this:

  • Reinstall EF with Nuget
  • Delete the .edmx file and recreate it with tables

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:

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;
<#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM