3

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?

Makla
  • 9,899
  • 16
  • 72
  • 142

1 Answers1

2

Yes, you can use query filters

No, you cannot use DbContext pooling

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • I added [feature request](https://github.com/aspnet/EntityFrameworkCore/issues/14625). – Makla Feb 07 '19 at 07:31
  • Now you can do it. https://learn.microsoft.com/en-us/ef/core/performance/advanced-performance-topics?tabs=with-di%2Cwith-constant#dbcontext-pooling – Abraham Velazquez Mar 12 '23 at 05:37