Is it possible to generate database-agnostic migrations with Entity Framework?

34 views Asked by At

I work on a web project, which uses Entity Framework to access a database. Right now, SQL Server Express LocalDB is used for development. The migrations created via dotnet ef migrations add ... contain T-SQL specific for SQL Server.

We want to use SQLite for local development, but changing the connection string now creates new migrations which are mixing SQL Server with SQLite syntax, making it impossible to use.

Since other ORMs (like Django) have their migrations in a database agnostic form, and generate the concrete SQL on the fly for the then configured database engine, does EF also support this kind of operation? If so, how can we enable this?

1

There are 1 answers

0
Guru Stron On

No, see this issue @github:

We are not planning to implement full scaffolding and management for migrations to multiple providers because the cost is very high while the value is relatively low compared to many other things. Also, sometimes multiple migration sets are appropriate, while sometimes a single migration is appropriate.

The are 2 workarounds:

  1. Create 2 different contexts inheriting from some base one which will hold all the corresponding DbSet's and setup (every child context will have it's own set of migrations)
  2. Move migrations to separate assemblies and create one such assembly per destination database type

See more: