问题
I have an app that uses EF Core and was originally written using V3.1.1. We have decided to take the plunge and update to v5.0.1 now its out of preview.
The app supports both MSSQL and SQLite and originally this worked pretty well. Migrations were created as mostly agnostic by the tooling and the documentation here provided 3 ways to use multiple databases:
- Create multiple context's and switch between them using the
--context
option todotnet ef migrations
. - Create multiple migration assemblies and select which one using the
MigrationsAssembly(...)
method. - Rely on the fact the migrations created by the tools are mostly agnostic and at the times you did need to do something DB specific, use methods in the
Migration
class to determine which DB you were running on and act accordingly.
We used option 3.
This resulted in a entity as follows:
public class LocalUser
{
// id etc removed
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Having a migration created as follows:
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true),
}
Which worked nicely on both MSSQL and SqlLite - without any need for any manual modification of the generated migration files.
Post the upgrade to v5.0.1 however, the same migration is now created like this (when run as configured to use MSSQL):
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
FirstName = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastName = table.Column<string>(type: "nvarchar(max)", nullable: true),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true),
}
Note the inclusion of the explicate type:
argument.
This now fails to run on SQLite with an error relating to "(max)" being a syntax error - which of course it is.
In addition, the documentation linked to above has been updated to remove any discussion of option 3 - the only two options discussed for multiple database support are multiple derived context's and multiple migration assemblies - both of which are a pain when you database is fairly simple and stores only core types, as ours does.
Does anyone know why this behaviour was changed and if its possible to restore its previous behaviour? (Ideally without having to explicitly set a column type on each and every property to something database agnostic like "text")
Thanks
回答1:
Sadly this has been confirmed as new intended behavior.
https://github.com/dotnet/efcore/issues/23714
Multiple migration assembles are now the way to go.
来源:https://stackoverflow.com/questions/65319430/entity-framework-core-output-db-agnostic-with-version-5