15

EF Core 1.1 and SQL Server 2016

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).

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.

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.

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

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

Adam
  • 3,872
  • 6
  • 36
  • 66
  • 1
    You can configure schema for Migration history table. Read more at http://www.bricelam.net/2017/01/04/efcore-history-table.html – Smit Sep 13 '17 at 23:32
  • This worked for us, thank you. Do you want to write it as a full answer for future reference to everybody? – Adam Sep 14 '17 at 19:34

1 Answers1

18

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

From the link,

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"));
Smit
  • 2,279
  • 1
  • 12
  • 22
  • 1
    Thank you, very good answer! One remark: If you don't need it, you can remove the `connectionString` parameter. In my case, it wasn't required. I was just moving `__EFMigrationsHistory` into its own schema 'ef', so `protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlServer(x => x.MigrationsHistoryTable("__EFMigrationsHistory", "ef"));` was doing it fine. – Matt Mar 30 '21 at 07:25
  • @Matt, your comment is just what I needed, removing the connection-string parameter--thanks! – Lee Cichanowicz Jul 12 '23 at 18:04