Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Load Exception in EF Core Project

Tags:

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

{ Stack trace }

Exception has been thrown by the target of an invocation.

like image 920
Steve Avatar asked Nov 29 '20 09:11

Steve


People also ask

Is lazy loading supported in EF core?

EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post. Blog and Blog. Posts navigation properties will be lazy-loaded.

What is lazy loading EF core?

Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.

Does EF core support EDMX?

EF Core does not support the EDMX file format for models. The best option to port these models, is to generate a new code-based model from the database for your application.


2 Answers

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

like image 79
Sajidur Rahman Avatar answered Sep 20 '22 11:09

Sajidur Rahman


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.

like image 21
Alexander Goldabin Avatar answered Sep 19 '22 11:09

Alexander Goldabin