【问题标题】:Migrate EF Migrations to EFCore将 EF 迁移迁移到 EFCore
【发布时间】:2018-05-03 14:32:10
【问题描述】:

我在当前的 .NET Framework 应用程序中进行了大约 40 次迁移。我正在将数据库及其所有存储库移植到 .NET 标准库,因为新应用程序是用 .NET Core 编写的,这会导致存储库的双重实现,并且在 .NET Core 应用程序中,我们必须使用 Dapper 才不会与任何东西发生冲突否则。

所以,我可以轻松地将IdentityDbContext 和所有其他相关内容移植到EntityFrameworkCore

但现在是迁移。我知道微软在从数据库中检索模型和 DbContext 时有一个document。但是,这会创建一个包含所有配置的非常长的 OnModelCreating 方法。看起来这可以工作但是,如果我想做任何新的更改,我必须在新的迁移中进行更改,并确保没有冲突。 此外,我们当前的迁移进行了手动调整,以确保它适用于新数据库或现有生产数据库。这种方式没有处理好一些事情。

我想要的是将现有的单个迁移文件迁移到新的代码语法。我不在乎是否省略了手动调整,但如果以某种方式自动转换最大的东西。

例如:

.NET 框架:

public override void Up()
{
    CreateTable(
        "dbo.AdditionalCosts",
        c => new
            {
                additionalCostId = c.Int(nullable: false, identity: true),
                shoppingCartId = c.Int(),
                shoppingCartProductId = c.Int(),
                orderId = c.Int(),
                inclPrice = c.Decimal(nullable: false, precision: 18, scale: 3),
                taxPrice = c.Decimal(nullable: false, precision: 18, scale: 3),
                description = c.String(),
                title = c.String(),
                costType = c.Int(nullable: false),
            })
        .PrimaryKey(t => t.additionalCostId)
        .ForeignKey("dbo.Orders", t => t.orderId)
        .ForeignKey("dbo.ShoppingCartProducts", t => t.shoppingCartProductId)
        .ForeignKey("dbo.ShoppingCarts", t => t.shoppingCartId)
        .Index(t => t.shoppingCartId)
        .Index(t => t.shoppingCartProductId)
        .Index(t => t.orderId);
}

.NET 核心:

是否有可用的转换工具?

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        "dbo.AdditionalCosts",
        c => new
        {
            additionalCostId = c.Column<int>(nullable: false), //idnetity: true?
            shoppingCartId = c.Column<int>(),
            shoppingCartProductId = c.Column<int>(),
            orderId = c.Column<int>(),
            inclPrice = c.Column<decimal>(nullable: false), //precision: 18, scale: 3?
            taxPrice = c.Column<decimal>(nullable: false), //precision: 18, scale: 3
            description = c.Column<string>(),
            title = c.Column<string>(),
            costType = c.Column<int>(nullable: false),
        }, constraints: table =>
        {
            table.PrimaryKey("PK_AdditionalCost", t => t.additionalCostId);//default naming of PK_? why not automated anymore! :(
            //etc..
        });
        //.PrimaryKey(t => t.additionalCostId)
        //.ForeignKey("dbo.Orders", t => t.orderId)
        //.ForeignKey("dbo.ShoppingCartProducts", t => t.shoppingCartProductId)
        //.ForeignKey("dbo.ShoppingCarts", t => t.shoppingCartId)
        //.Index(t => t.shoppingCartId)
        //.Index(t => t.shoppingCartProductId)
        //.Index(t => t.orderId);
}

【问题讨论】:

    标签: c# entity-framework .net-core entity-framework-core .net-standard


    【解决方案1】:

    我最终有几个选择:

    1. 检查每个表,获取正确的索引名称、列名称并自己创建迁移文件。
    2. 使用代码回到过去并使用 EFCore 再次生成每个迁移。
    3. 使用当前代码生成一个迁移,并尝试以某种方式对齐当前数据库。
      • 问题:EFCore 生成索引和某些 FK 名称的方式不同。

    所以.. 最终,我使用当前代码生成了一个新的迁移,在 Visual Studio 中进行了架构比较,以检查真正的差异,并在需要时对您的迁移进行调整。然后,在发布时将当前数据迁移到新的数据库中。我目前正在这样做,所以我可能会稍后调整这个答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 2022-01-03
      • 2017-10-25
      • 1970-01-01
      • 2021-11-23
      • 2021-05-04
      • 2020-01-31
      • 1970-01-01
      相关资源
      最近更新 更多