-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-tenant applications (model building and migrations) #96
Comments
Any update on this? I understand that multi-tenancy has not been accepted as within the scope of EF Core, but nonetheless people are constantly asking questions on here about this. Specifically on a database-per-tenant approach: After looking through the ef tool I don't see why it wouldn't be possible for a user to make a "mass update" command to call We appreciate you working with those of us who are implementing our own solutions, it may be best to explicitly state that multi-tenancy is not supported but is available via libraries or on help-sites. |
@Perustaja No update, but the issue is not closed, which means we still plan to have guidance at some point. |
I've read countless articles over the last month trying to find a way to achieve this before stumbling onto this post and wish I'd seen this first and saved the time! I assumed there was already a way to accomplish migrations with multiple connection strings. There seems to be various methods for using multi tenant databases which is was surprised me that I couldn't use migrations. Seeing as this is now over 5 years old and still no information, I'll look into alternate providers / methods. |
I found a good and simple solution for this problem. (I like the title of dotnet/efcore#4004 better but I see the discussion continues here) The scenario: Steps:
public class SchemaAwareMigrationsSqlGenerator : NpgsqlMigrationsSqlGenerator
{
private readonly string _schema;
public SchemaAwareMigrationsSqlGenerator(
MigrationsSqlGeneratorDependencies dependencies,
INpgsqlOptions npgsqlOptions
) : base(dependencies, npgsqlOptions)
{
var dbContext = dependencies?.CurrentContext?.Context;
_schema = getTenantCodeFromDbContext(dbContext);
} NB: The code in the constructor will be the same regardless of your DB provider. This is because the constructor will always include the
protected override void Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
{
var schemaProp = operation.GetType().GetProperty("Schema");
if (schemaProp != null && schemaProp.CanWrite)
{
schemaProp.SetValue(operation, _schema);
}
// Updating 'NewSchema' is optional but you may likely need it
schemaProp = operation.GetType().GetProperty("NewSchema");
if (schemaProp != null && schemaProp.CanWrite)
{
schemaProp.SetValue(operation, _schema);
}
base.Generate(operation, model, builder);
}
var options = new DbContextOptionsBuilder<YourSchemaAwareDbContext>()
// ...
.ReplaceService<IMigrationsSqlGenerator, SchemaAwareMigrationsSqlGenerator>(); Addendum: |
After a long week reading nearly everything I could find about tenant by schema in EF Core I found this post and it helped me go 5 steps further. One thing to add frmo the last posts. For the foreign keys the SchemaAwareMigrationsSqlGenerator needs also the
|
See question in dotnet/efcore#4004 as an example of what we should cover
The text was updated successfully, but these errors were encountered: