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

entity-framework entity-framework-core

Question

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:

  • can I use context pooling with this approach
  • can I use IMutableEntityType.QueryFilter set in OnModelCreating

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?

1
0
2/6/2019 12:28:16 PM

Accepted Answer

Yes, you can use query filters

No, you cannot use DbContext pooling

1
2/6/2019 2:16:15 PM


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