You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common mutli-tenant scenario involves using model that only differs by schema. Currently this requires a different model for each tenant. If the model could be parameterized somehow so that the schema used is different depending on the context/connection/tenantid/etc, then this could allow the same model to be used for multiple tenants, which would also mean that needing to change the model cache key would not be needed. We should also consider how this interacts with reverse engineering and migrations. See #9864 for a particular case where this could be useful.
Reacted by Paya, Marco Antonio Regueira, Sirapop Chaimanasuksri, Vasim Hanna-Salem, Victor Gladkikh, Andrii Petryna, roxk, Gunnar Stensby, endrihacker, Łukasz and 17 more
This future will be really useful for us.
I was using a multi-tenant configuration in ef core 1.x now i am trying to port it to ef core2 with truble because we were using a lot of workaround to do the job.
In my scenario i have a lot of per-tenant tables and few tables (like User account) shared by all tenants.
With a context factory class and a customized Context class i can build a context with only shared tables or a context with all tables (shared and perTanant) for the requested tenant .
protected void CustomCreateModel(string tenantSchema, ModelBuilder builder, bool sharedOnly)
{
if (sharedOnly)
{
builder.Ignore<RoomPerUser>();
builder.Entity<UserProfile>().Ignore(p => p.MyRooms);
}
base.OnModelCreating(builder); //CREATE MODEL FOR IdentityDbContext
builder.Entity<UserCompany>();
if (!sharedOnly)
{
builder.Entity<RoomPerUser>().HasOne(r => r.UserProfile;
[...]
}
}
When the model is used by migration an IDbContextFactory read parameter "EnvironmentName" and create the full model or the shared_only depending on param value. This is a stupid workaround i have used to call AddMigration working with separate file for SharedOnly tables. In this way when migration create the Files i can create a sqlScript and use it in a loop for each tenant.
When the model is used by the asp.net core application the IModel is compiled and cached in a concurrent dictionary with tenant code as the key. This was obviously to avoid to do the hardest compiling job for each instance of Context.
public class DbOptionSetter : IDbOptionSetter
{
[..]
public void SetDbContextOptionsBuilder(string tenant, bool sharedEntitiesOnly, DbContextOptionsBuilder optionsBuilder)
{
var cachedOptions = _optionsCache.GetOrAdd(tenant,
t =>
{
_logger.LogDebug(LoggingEvents.CREATE_NEW_IMODEL, "Create new IModel for tenant:[{1}]", tenant);
return MyContext.GetModel(tenant, sharedEntitiesOnly);
});
optionsBuilder.UseModel(cachedOptions);
optionsBuilder.UseSqlServer(ConnectionString, b => b.MigrationsAssembly(TMP_MIGRATION_ASSEBLY));
}
}
This code was for ef core 1.x now i am porting it to ef core 2.0
Activity
ste4net commentedon Oct 9, 2017
This future will be really useful for us.
I was using a multi-tenant configuration in ef core 1.x now i am trying to port it to ef core2 with truble because we were using a lot of workaround to do the job.
In my scenario i have a lot of per-tenant tables and few tables (like User account) shared by all tenants.
With a context factory class and a customized Context class i can build a context with only shared tables or a context with all tables (shared and perTanant) for the requested tenant .
When the model is used by migration an IDbContextFactory read parameter "EnvironmentName" and create the full model or the shared_only depending on param value. This is a stupid workaround i have used to call AddMigration working with separate file for SharedOnly tables. In this way when migration create the Files i can create a sqlScript and use it in a loop for each tenant.
When the model is used by the asp.net core application the IModel is compiled and cached in a concurrent dictionary with tenant code as the key. This was obviously to avoid to do the hardest compiling job for each instance of Context.
This code was for ef core 1.x now i am porting it to ef core 2.0
Thank you
[-]Allow parts of model to be parameterized--e.g. schema[/-][+]Schema-based multi-tenancy[/+]