简体   繁体   English

多个数据库上的 Entity Framework Core 迁移

[英]Entity Framework Core migrations on multiple databases

I am creating a .NET 6 API using EF core with SQL Server, and trying to implement multiple databases that would different connection strings based on an id that is passed in from an identity token.我正在创建一个 .NET 6 API 使用 EF 核心和 SQL 服务器,并尝试实现多个数据库,这些数据库将根据从身份令牌传入的 id 使用不同的连接字符串。 Essentially, there would be multiple databases that each contain the same tables, but storing different information.本质上,会有多个数据库,每个数据库都包含相同的表,但存储不同的信息。 A user would be able to edit data from the front end and based on the "tenant" that they are working in, it would store the data in the appropriate database.用户将能够从前端编辑数据,并根据他们正在使用的“租户”,将数据存储在适当的数据库中。 For the time being, all of the databases will be on the same server.目前,所有数据库都将位于同一台服务器上。

When a user makes a request, I have been able to implement that correctly, using the following inside of my DbContext:当用户发出请求时,我已经能够正确地实现它,在我的 DbContext 中使用以下内容:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if(!optionsBuilder.IsConfigured)
            {
                var tenantId = _httpContext.GetClaim("tenantId");
                optionsBuilder.UseSqlServer(GetClientConnectionString(tenantId));
            }
        }

where the GetClientConnectionString function would perform some logic to manipulate the default connection string and return the correct value based on that id from the user token.其中GetClientConnectionString function 将执行一些逻辑来操作默认连接字符串并根据用户令牌中的该 ID 返回正确的值。 This works fine and when run with the appropriate token, is able to switch connection strings ok.这工作正常,当使用适当的令牌运行时,能够正常切换连接字符串。

The part I am unsure of is how to maintain each database individually - is there a good way to run migrations for each of the databases?我不确定的部分是如何单独维护每个数据库——是否有为每个数据库运行迁移的好方法? I know that a connection string can be passed in to the do.net ef migrations... command, however if the amount of databases grows to a decent number this does not seem efficient.我知道可以将连接字符串传递给do.net ef migrations...命令,但是如果数据库数量增长到相当大的数量,这似乎效率不高。

Is the best bet to just bite the bullet and perform the migrations manually?硬着头皮手动执行迁移是最好的选择吗? Or would it be possible to somehow loop through a collection of keys which would return the connection string value and apply the migration to each?或者是否有可能以某种方式循环遍历一组键,这些键将返回连接字符串值并将迁移应用于每个键?

I am relatively new to anything more than a simple dataset in EF so I am hoping I am not just missing something.我对 EF 中的简单数据集以外的任何东西都比较陌生,所以我希望我不仅仅是遗漏了一些东西。

Thanks in advance提前致谢

I have never done this, but You can try start migration for all dbs with one DbContext(said that the bases are the same), just try to change the connection string for each db.我从来没有这样做过,但是您可以尝试使用一个 DbContext 开始迁移所有数据库(说基础是相同的),只需尝试更改每个数据库的连接字符串即可。 I don't have time to test it, but I hope it will work我没有时间测试它,但我希望它会起作用

foreach (var connection in ConnectionList)
{
    var optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
    optionBuilder.UseSqlServer(connection);

    using var dbContext = new ApplicationDbContext(optionBuilder.Options);
    var pendingMigrations = await dbContext.Database.GetPendingMigrationsAsync();

    if(pendingMigrations.Any())
    {
        await dbContext.Database.MigrateAsync();
    }

    await dbContext.DisposeAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码首先迁移多个数据库 - Entity Framework code first migrations with multiple databases 为 Entity Framework Core 中的所有数据库创建和应用迁移 - Create and apply migrations for all databases in Entity Framework Core 实体框架核心:创建合并多个DbContexts的迁移(具有多个迁移) - Entity Framework Core: create a migration that merges multiple DbContexts (with multiple migrations) 通过 Entity Framework Core 将多个数据库连接到 .NET 核心项目 - Connect multiple Databases to .NET core project via Entity Framework Core 测试实体框架核心迁移 - Testing Entity Framework Core Migrations 使用迁移与实体框架核心 - Using Migrations with Entity Framework Core 具有多个数据库的实体框架 - Entity Framework with multiple databases 如何在同一项目中使用 2 个不同的数据库架构组织 Entity Framework Core 迁移(代码优先)? - How to organize Entity Framework Core migrations (code first) with 2 different databases schema in the same project? 使用UseInMemoryDatabase的实体框架核心迁移错误 - Entity Framework Core migrations error using UseInMemoryDatabase Entity Framework Core 2.0.0迁移在不同的项目中 - Entity Framework Core 2.0.0 Migrations in different project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM