entity-framework.net-coreentity-framework-coresqlmigrations

Update database from models using entity framework in multi tenant .net core


I am using .net core entity framework.

I have multi tenant dbs. so I have kept one root tenant db as base. and I want to replicate those schema changes to all other dbs using entity framework. I am generating my models using following command.

Scaffold-DbContext "Data Source=(local);Initial Catalog=sampleTenantDb;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Tenants -Force

So when creating new tenant, I simply use

context.Database.EnsureCreated();

But when I add new table in code, I want to apply it to all tenants. So how do I do it?

I tried following but, it doesn't work (not adding remanining tables)

myDbContext.Database.Migrate();

Solution

  • If your DbContext is created with Scaffold-DbContext, it won't have any Migrations. That's for a database-first workflow, in which you would apply the DDL changes to the tenant databases with a script, perhaps created with SQL Server Data Tools.