Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

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.

like image 221
Jack He Avatar asked Jul 26 '14 03:07

Jack He


People also ask

Can Entity Framework 6 be used against Microsoft SQL Server?

For example, one provider can be plugged in to allow EF to be used against Microsoft SQL Server, while another provider can be plugged into to allow EF to be used against Microsoft SQL Server Compact Edition. The providers for EF6 that we are aware of can be found on the Entity Framework providers page.

What is the Entity Framework provider model?

The Entity Framework provider model allows Entity Framework to be used with different types of database server. For example, one provider can be plugged in to allow EF to be used against Microsoft SQL Server, while another provider can be plugged into to allow EF to be used against Microsoft SQL Server Compact Edition.

What is the dbproviderservices class in Entity Framework?

In older versions of EF the DbProviderServices class was part of the .NET Framework and was found in the System.Data.Common namespace. Starting with EF6 this class is now part of EntityFramework.dll and is in the System.Data.Entity.Core.Common namespace.

What is a targetinvocationexception and dependencyresolutionexception?

[TargetInvocationException: Exception has been thrown by the target of an invocation.] [DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Exception has been thrown by the target of an invocation.


1 Answers

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.

like image 92
Anton Lyhin Avatar answered Oct 24 '22 00:10

Anton Lyhin