gpt4 book ai didi

.net-core - EF Core 多个迁移集

转载 作者:行者123 更新时间:2023-12-03 08:23:44 24 4
gpt4 key购买 nike

我目前正在使用 EF Core Code First Migrations 开发 .Net Core 应用程序。我在 Windows 和 OS X 之间跳来跳去进行我的开发(在家中使用 Windows,在旅途中使用 Mac),我试图弄清楚如何在使用 Windows 时使用 SQL Server 的数据库迁移,而在使用 SQLite 时使用 SQLite苹果电脑。

是否可以维护两组迁移并选择要应用的组(无需单独指定每个迁移)?或者我是否需要创建单独的 .Net Core 应用程序/程序集并指定 --assembly dotnet ef database update 的选项命令?

最佳答案

您有两个选择:

  • 使用两组迁移
  • 手动将迁移编辑为与提供者无关

  • 多套

    每个集合都需要在其自己的程序集中。首先,生成第一个迁移并将其移动到新项目中。您将在配置提供程序时配置迁移程序集。

    optionsBuilder.UseSqlServer(
    mssqlConnectionString
    , x => x.MigrationsAssembly("MyApp.Migrations.SqlServer"));
    //optionsBuilder.UseSqlite(
    // sqliteConnectionString,
    // x => x.MigrationsAssembly("MyApp.Migrations.Sqlite"));

    我有 a sample将迁移放在它们自己的程序集中,您可能还想将其用作引用。

    供应商不可知

    要使迁移提供者不可知,请合并每个提供者将生成的内容。例如,以下对 SQL Server 和 SQLite 都有注释。

    migrationBuilder.CreateTable(
    name: "People",
    columns: table => new
    {
    Id = table.Column<int>(nullable: false)
    .Annotation("Sqlite:Autoincrement", true)
    .Annotation("SqlServer:ValueGenerationStrategy",
    SqlServerValueGenerationStrategy.IdentityColumn)
    },
    constraints: table =>
    {
    table.PrimaryKey("PK_People", x => x.Id);
    });

    关于.net-core - EF Core 多个迁移集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819371/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com