Makla
Makla

Reputation: 10459

EF Core with DbContext pool and multi tenancy - database per tenant

I need to solve problem with multi tenancy in entity framework core. Each tenant gets own database (for historical reasons, this can not be changed).

I will use context factory as suggested on many places (example 1, example 2, example 3).

My questions are:

If DbContext is returned from pool, I am worrying that will link to old connection string, not new (if request was started from different tenant then where context was created).

I think I can solve migrations like this (from second point):

foreach (var tenant in allTenants)
{
    var context = dbContextFactory.CreateDbContext(tenant, configuration);
    context.Database.Migrate();
}

Besides my 2 questions. Is there anything else I should thought before start coding?

Upvotes: 4

Views: 2138

Answers (1)

ErikEJ
ErikEJ

Reputation: 41749

Yes, you can use query filters

No, you cannot use DbContext pooling

Upvotes: 2

Related Questions