Trying to create a code first project using Entity Framework and MySQL.
When I use context.Database.EnsureCreated(); the tables are created correctly, but I would like to use migrations so the code changes to: context.Database.Migrate(); and that is when I get the error:
MySqlException: Table 'library.publishers' doesn't exist
I do see that the database was created and there is an empty table: __efmigrationshistory but that is the only table, no publishers like it does with the EnsureCreated.
What Am I missing here?
Here is the minimal code reproducing the error:
using Microsoft.EntityFrameworkCore;
namespace mySqlEFcore
{
class Program
{
static void Main(string[] args)
{
using (var context = new LibraryContext())
{
context.Database.Migrate();
context.Publishers.Add(new Publisher { ID = 1 });
context.SaveChanges();
}
}
private class Publisher
{
public int ID { get; set; }
}
private class LibraryContext : DbContext
{
public DbSet<Publisher> Publishers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("server=localhost;database=library;user=root;password=123456;SslMode=none;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Publisher>(entity => { entity.HasKey(e => e.ID); });
}
}
}
}
Tried running Add-Migration InitialCreate but ran into more errors ...
Added a reference Microsoft.EntityFrameworkCore.Design and now the InitialCreate shows:
System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory..ctor(Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger'1, Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)'. at MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLCommandBuilderFactory..ctor(IDiagnosticsLogger'1 logger, IRelationalTypeMapper typeMapper)
Pomelo.EntityFrameworkCore.MySqlinstead, that works fine!Add-Migration InitialCreatethat should create everything... take a look on my project for the versions