13
votes

I have an ASP.NET Core 3.1 Web API application using EF Core. This is the my configuration in the ConfigureServices method of the Startup class:

services.AddDbContext<ApplicationContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppConn")));

The above configuration tested and works for a SQL Server database.

Then I switched to using Sqlite after installing its package successfully.

services.AddDbContext<ApplicationContext>(options =>
     options.UseSqlite("Data Source=sqlitedemo.db"));

But when I try to add the EF migration

add-migration initial -context ApplicationContext

I get this error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer' from assembly 'Microsoft.EntityFrameworkCore, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

at Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(IOperationReportHandler reportHandler, IDictionary args)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language)
at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute()
at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.b__0()
at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

Exception has been thrown by the target of an invocation.

5
You seem to have EF Core related package versions mismatch. Please include the EF Core related project package references (all entries from .csproj staring with <PackageReference Include="Microsoft.EntityFrameworkCore.)Ivan Stoev

5 Answers

15
votes

Please update to your entity framework core nuget package to 3.1.10(or latest 5.0.0). It will solve your problem.

8
votes

I had EF Core 5 package installed, but not Microsoft.EntityFrameworkCore.Design and one package were implicitly referencing older version (Microsoft.EntityFrameworkCore.Design 3.0.0).

Installing explicit dependency on Microsoft.EntityFrameworkCore.Design 5.x.x resolved the issue for me.

3
votes

Do not forget that for migrations, you must have the packages installed both in the main EntityFramework project and in the startup project.

Microsoft.EntityFramework.Core
Microsoft.EntityFramework.Core.Design
1
votes

I had this issue in a .NET Core 3.1 project. I fixed it by installing the package Microsoft.EntityFrameworkCore.Design in the startup project

1
votes

I had a similar issue when all the dependencies seemed correct in my project. But the issue was that the context was in one project in the solution and a different project in the solution was the start-up project. And that project happened to include a different version of EFCore.

Using the context's project as the start-up project in the Package Manager Console resolved the issue for me.

add-migration -startupproject myProjectWithTheMigration