具有多个数据库架构的 EF Core 迁移 [英] EF Core Migrations with Multiple DB Schemas

本文介绍了具有多个数据库架构的 EF Core 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF Core 1.1 和 SQL Server 2016

EF Core 1.1 and SQL Server 2016

我们正在运行一个微服务应用程序,其中一些微服务具有独立的几个表.为单个微服务拥有多个表的解决方案之一是为这些服务提供一个独特的架构(例如不是 DBO)并将它们全部放在一个数据库中(有利于成本 + 维护).

We are running a microservice application with some microservices having independent few tables. One of the solutions to have many tables for individual microservice, is to give these services a unique schema (e.g. not DBO) and put all of them in one database (good for cost + maintenance).

这在 EF 6 中运行良好,但是,查看核心中生成的 dbo.__EFMigrationsHistory,看起来核心没有考虑架构.

That works fine in EF 6, however, looking at the generated dbo.__EFMigrationsHistory in core, it looks like core doesn't take the schema into account.

我知道 EF Core 中的迁移已更改为查看代码而不是数据库,但我的问题是在 dbo.__EFMigrationsHistory 表中记录的版本与模式无关.

I am aware that the migration in EF Core has changed to look in the code rather than the DB, but my problem is the version that is recorded in the dbo.__EFMigrationsHistory table is schema-agnostic.

您知道我们如何在将迁移写入数据库时​​让 EF Core 架构感知吗?

Do you know how can we make EF Core schema-aware when it writes the migration to the db?

注意builder.HasDefaultSchema(Constants.SCHEMA); 已在 DBContext 中设置.

N.B. builder.HasDefaultSchema(Constants.SCHEMA); is already set in the DBContext.

推荐答案

builder.HasDefaultSchema() 用于设置模型的模式.MigrationHistory 表的配置略有不同.您可以在此处

builder.HasDefaultSchema() is used to set schema for model. MigrationHistory table is configured bit differently. You can read more about it here

来自链接,

最简单的情况是您只想更改表名或架构.这可以使用 OnConfiguring(或 ASP.NET Core 上的 ConfigureServices)中的 MigrationsHistoryTable 方法来完成.这是一个例子.

The simplest scenario is when you just want to change the table name or schema. This can be done using the MigrationsHistoryTable method in OnConfiguring (or ConfigureServices on ASP.NET Core). Here is an example.

protected override void OnConfiguring(DbContextOptionsBuilder options)
    => options.UseSqlServer(
        connectionString,
        x => x.MigrationsHistoryTable("__MyMigrationsHistory", "mySchema"));

这篇关于具有多个数据库架构的 EF Core 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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