Entity Framework Core 2.0, migrations for both SQL Server and Postgres?

Viewed 1967

Using Entity Framework Core 2.0, how can I in the same solution keep migrations for both Microsoft SQL Server and Postgres if my application needs to be able to target both types? In my case if running on MacOS I want to use Postgres, and if running on Windows I want to use SQL Server. Before setup of DataContext we determine platform using calls to "RuntimeInformation.IsOSPlatform(..)" and extract the appropriate connection string.

Currently the solution is separated into one ASP.NET Core project and one "Data" project which keeps the DataContext and migrations for SQL Server only.

My attempts, that have failed, have so far been:

  1. Create separate projects for migrations, one for the SQL Server migrations and one for the Postgres migrations. Then run "ef migrations add ..." with the "-p" parameter pointing to the migrations project I want to use. This is done in conjunction with call to either ".UseNpgsql(connectionString, o => o.MigrationsAssembly(..)" or ".UseSqlServer(connectionString, o => o.MigrationsAssembly(..)". This fails with the tooling complaining that there is no DataContext found.

  2. Same as "1." but we also add a class implementing "IDesignTimeDbContextFactory<..>" in each of the migrations projects. This fails with the tooling complaining that there is no DataContext found.

  3. Keep SQL Server migrations in the same project as the DataContext. Invoke "ef migrations add .." with the "-o" parameter specifying another output folder for the migrations for Postgres. This works for the SQL Server migrations, but fails for the Postgres migrations by way of that it does not like if a migration exists in the same project with the same name (even if it's in another folder), and also the migrations are generated for SQL Server and not for Postgres, even if they are put in the specified folder.

Is this a currently not supported scenario and we should stick with Postgres on all platforms? (which does work).

0 Answers
Related