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

.net-core entity-framework entity-framework-core sqlmigrations

Question

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();
1
0
2/29/2020 1:41:18 PM

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

1
2/29/2020 6:33:16 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow