I am developing a C# project using EF Core. The solution contains two projects:
• Project A: a WinForms project to run the application
• Project B: a project to manage a SQLite-database
Project B contains the DbContext, migrations an so on.
DbContext-Class:
public class ContextTest : DbContext
{
public DbSet<LogTest> LogTests { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
string sqlitePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.db");
options.UseSqlite($"Data Source={sqlitePath}");
}
}
Class for executing the migrations:
public class DbMigrator
{
public static void ExecuteMigrations() {
using (var context = new ContextTest())
{
context.Database.Migrate();
}
}
}
If I configure project B as the startup project, I can do the migrations without any problems.
But if project A is set as the start project, I get a System.NullReferenceException as soon as context.Database.Migrate();
is called.
I have tried already to set the MigrationsAssembly:
options.UseSqlite($"Data Source={sqlitePath}",
o => o.MigrationsAssembly(typeof(ContextTest).Assembly.GetName().Name));
It didn't help. I have to call up migrations from Project A. Maybe someone has an idea?