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

363 Views Asked by At

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();
2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

You have need to create multiple dbcontext object and and pass connection string in that way you can update multiple db