实体框架核心-与版本5以上版本无关的输出数据库 [英] Entity Framework Core - Output DB agnostic with version 5+

查看:59
本文介绍了实体框架核心-与版本5以上版本无关的输出数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用EF Core的应用程序,最初是使用V3.1.1编写的.我们已决定尝试一下,并将其更新到v5.0.1,现在它已脱离预览.

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.

该应用程序同时支持MSSQL和SQLite,并且最初运行良好.该工具和文档这里提供了3种使用多个数据库的方式:

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:

  1. 创建多个上下文,并使用-context 选项在它们之间切换,以进行 dotnet ef迁移.
  2. 创建多个迁移程序集,然后使用 MigrationsAssembly(...)方法选择其中一个.
  3. 依靠这些工具创建的迁移几乎是不可知的事实,并且当您确实需要执行特定于DB的操作时,请使用 Migration 类中的方法来确定要在哪个DB上运行并采取相应行动.
  1. Create multiple context's and switch between them using the --context option to dotnet ef migrations.
  2. Create multiple migration assemblies and select which one using the MigrationsAssembly(...) method.
  3. 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.

我们使用了选项3.

这导致一个实体如下:

public class LocalUser
{
    // id etc removed
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

创建的迁移如下:

 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),
            }

在MSSQL和SqlLite上都能很好地工作-无需对生成的迁移文件进行任何手动修改.

Which worked nicely on both MSSQL and SqlLite - without any need for any manual modification of the generated migration files.

将升级发布到v5.0.1之后,现在会创建相同的迁移(在配置为使用MSSQL运行时):

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),
            }

请注意包含重复的 type:参数.

Note the inclusion of the explicate type: argument.

此操作现在无法在SQLite上运行,并显示与(max)"相关的错误.是语法错误-当然是.

This now fails to run on SQLite with an error relating to "(max)" being a syntax error - which of course it is.

此外,上面链接的文档已更新,以消除对选项3的任何讨论-为多数据库支持而讨论的仅有的两个选项是多个派生上下文和多个迁移程序集-当您使用数据库时,这两者都是很麻烦的相当简单,并且只存储我们的核心类型.

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.

有人知道为什么更改此行为以及是否有可能恢复以前的行为吗?(理想情况下,不必在每个属性上为每个与数据库无关的东西(例如"text")显式设置列类型)

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")

谢谢

推荐答案

不幸的是,这已被确认为新的预期行为.

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.

这篇关于实体框架核心-与版本5以上版本无关的输出数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆