9

I am newer about using Code First in c#. After I enabled Migration in my project and launch my site, I get an Error:

Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' from assembly 'EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' does not have an implementation.

I have defined the context class as below.

namespace MyOA.Migration.Contexts
{
    public class OADBContext : DbContext
    {
        public OADBContext() { }
    }
}

and I tried to create the DB in Global.asax as below.

protected void Application_Start()
{
    // Forces initialization of database on model changes.
    using (var context = new Migration.Contexts.OADBContext())
    {
        context.Database.Initialize(force: true);
    }    
}

I tried to search the reason but got no idea. Any suggestions? Thanks in advance.

User 12345678
  • 7,714
  • 2
  • 28
  • 46
Jack He
  • 1,683
  • 3
  • 18
  • 29

5 Answers5

4

If you check the .NET version of the two asseblies:

  • EntityFramework (v4.5)
  • EntityFramework.SqlServer (v4.0)

You will see that EntityFramework.SqlServer has v4.0 .NET dependency, but EntityFramework uses v4.5. That is the root of the issue. I use dotpeek tool for checking the assembly version (there are other options from stack overflow to check .net vestion of an assembly).

Note: and really when you decompile EntityFramework.SqlServer.dll using jetBrains reflector tool you will find that there is no ExecuteAsync method.

What we have to do to fix the issue is to use nuget.exe (from visual studio or from stand alone nuget executable: please find "latest nuget.exe"). And run it from command line:

cd "[path to your nuget.exe]"
nuget Install EntityFramework

A set of EF 6 assemblis will be downloaded. Use the EntityFramework and EntityFramework.SqlServer from .net v4.5 folder.

Community
  • 1
  • 1
Anton Lyhin
  • 1,925
  • 2
  • 28
  • 34
0

I +1'd @Spirit's answer for pointing me at the root of the issue, but I was able to fix it for myself by searching through all of the *.csproj files in the solution and replacing

packages\EntityFramework.6.1.3\lib\net40\EntityFramework.dll
                                   ^^^^^

with

packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll
                                   ^^^^^

I had a mix of usages in the projects. My symptom was that NCruch would get the exception on a test, but running the same test from NUnit test runner would pass just fine.

tehDorf
  • 775
  • 1
  • 11
  • 32
0

I faced this issue in my work, and tried suggested solutions with no luck, but the solution that worked out for us was to use a relatively older version of Entity Framework, we used version 6.0.0 while the latest (as of the time I'm writing down this answer) is 6.1.3

The issue with me was that my project was targeting .Net Framework 2.0, and suddenly when upgraded to .Net Framework 4.6.1 a warning appeared so I tried to update Entity Framework package which was on the latest 6.1.3, but it stopped working properly, and got the exception mentioned above.

Hope this will help somebody later.

0

I had this same problem but the cause was different. In the "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files" folder, there were several versions of the entityframe dlls. No doubt older versions. I had already recycled the iis pool and restarted the computer but that didn't work. I had to delete all the files in that folder and that did the trick.

Jason Cheng
  • 180
  • 2
  • 12
0

Since all similar S/O questions are being directed to this one, I'll leave another idea to consider. Check to make sure that EntityFramework hasn't been installed into you GAC by chance. IIS will take the GAC version over your bin version leading to some serious head scratching.

chprpipr
  • 2,039
  • 16
  • 17