Dot Net developer
Dot Net developer

Reputation: 436

Unable to generate database tables from Migration MySQL Ef-Core

I am trying to generate database and tables using EF Core Migrations. I have a Class Library Project which has all the necessary entities and migration datas . I am referencing Class Library Project in another ASP.NET Core 2.1 Web Application . When running

add-migration initial

command in Nuget Package Manager Console choosing Class Library Project, Migration file is generated in Class Library Project which is fine.

When I run command

update-database

choosing both class libary and web project, I get the message stating that :

No migrations were applied. The database is already up to date.

My DbContextClass:

 public class UserDbContext:DbContext
    {
        public UserDbContext(DbContextOptions<UserDbContext> options) : base(options){}
        public DbSet<LoginSession> login_sessions { get; set; }
        public DbSet<Authentication> authentications { get; set; }
        public DbSet<RolePermissionMap> role_permission_maps { get; set; }
        public DbSet<Entity.User> users { get; set; }
        public DbSet<Role> roles { get; set; }
        public DbSet<UserRole> user_roles { get; set; }
    }

I tried running

dotnet ef database update

command from console choosing Class Library project, I got an error message saying:

The specified framework version '2.1' could not be parsed The specified framework 'Microsoft.NETCore.App', version '2.1' was not found.

When I copied all important files from Class Library to another web application project i.e, migration file and web project in same file, everything works fine.

I am wondering why I got that error and unable to find solution when class library and web application are in separate files. Can anyone help me? I have many class libraries referenced in web application. So, I cannot port all the necessary files to another web application just to generate database tables.

Upvotes: 1

Views: 132

Answers (1)

paulsm4
paulsm4

Reputation: 121871

I've run into similar issues before.

Two suggestions:

  1. Please consider nuking your entire "Migration" (all directories, files, etc) from your project, then re-running dotnet ef migrations add..., followed by dotnet ef database update...

  2. Consider using the Pomelo data provider for MySQL:

https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql

Upvotes: 1

Related Questions