Entity Framework Core代码迁移可以针对不同的数据库类型可靠且实际地工作吗 [英] Can Entity Framework Core code migrations work reliably and practically against different database types

查看:64
本文介绍了Entity Framework Core代码迁移可以针对不同的数据库类型可靠且实际地工作吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下,这是我正在使用的核心版本实体框架。

I would like to clarify that this is the Core version Entity Framework that I'm working with.

我想我知道答案,但这都是基于我几年前使用的其他ORM的假设。我的猜测是,答案类似于它取决于特定的数据库和所使用的功能(键,约束等)。

I think I know the answer, but it's all based on assumptions from other ORMs I used years ago. My guess is the answer is something like "it depends based on the specific database(s) and the features used (keys, constraints, etc)".

另外,我想澄清一下,我不是在问是否可以从模型为不同的数据库生成迁移。我知道答案是肯定的。我要问的是已经生成的迁移的单个集合。

Additionally, I want to clarify that I'm not asking if migrations can be generated from the model for different DBs. I know the answer is yes to that. I'm asking about a single set of already generated migrations.

推荐答案

单个迁移可以 >应用于不同的数据库类型。该工具没有太大帮助(问题#1825 ),因此您可能需要动手-编辑迁移文件。

A single set of migrations can be applied to different database types. The tooling doesn't help much (issue #1825), so you may need to hand-edit the migration files.

是的,这确实取决于数据库支持的功能,因此,阻力最小的方法是使用跨平台可用的最低通用功能集提供者。您可以使用注释和 ActiveProvider 属性来影响特定于提供程序的配置。例如:

Yes, it does depend on what features the databases support, so the path of least resistance is to use the lowest common set of features available across the providers. You can use annotations and the ActiveProvider property to influence provider-specific configuration. For example:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<int>(
        table: "Blogs",
        name: "Id",
        nullable: false)
        // Use AUTOINCREMENT on SQLite
        .Annotation("Autoincrement", true)
        // Use IDENTITY on SQL Server
        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

    if (ActiveProvider == "Microsoft.EntityFrameworkCore.SqlServer")
    {
        migrationBuilder.CreateSequence("SomeSequence");
    }
}

这篇关于Entity Framework Core代码迁移可以针对不同的数据库类型可靠且实际地工作吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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