Skip to content
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

Migrations: How to have multi tenant application with same model in different schemas #4004

Closed
rmja opened this issue Dec 8, 2015 · 7 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@rmja
Copy link

rmja commented Dec 8, 2015

I have a multi tenant application, where the same dbcontext, TenantDbContext is used for each tenant, and where each tenant uses a different schema.
I am doing this by specifying the schema name in the TenantDbContext constructor, and in OnModelCreating setting modelBuilder.HasDefaultSchema(TenantSchema). When I use the TenantDbContext this is silently hidden by a custom DI factory for the context.

The problem comes when I want to do migrations, as I only have one DbContext seen from "dnx ef dbcontexts list", namely the general one.

I was wondering if it is possible somehow to make a placeholder for the schemaname when the migration is added, which is later replaced during the individual database updates.

I was thinking along the way:

dnx ef migrations add aMigration
dnx ef database update --replace-schema "dbo" "tenant1"
dnx ef database update --replace-schema "dbo" "tenant2"

etc. The --replace-schema should, when applying the migration, take all objects in the migration on dbo and do the migration on the tenant1 schema instead.

This would meen that the same migration is applied multiple times to the same database which might be a problem.

Any input on how to accomplish this, or maybe something similar?

@MaximBalaganskiy
Copy link

If you let an instance of a web site run context.Database.Migrate() at that moment the context would already be constructed with the correct schema. If you want to manually run migrations for all tenants, I think, a custom command is the answer.

@rmja
Copy link
Author

rmja commented Dec 13, 2015

@MaximBalaganskiy I've tried that, but the problem is that migrations do not honor the modelBuilder.HasDefaultSchema(TenantSchema) that I have in OnModelCreating(), and so the migrations are applied to the dbo schema instead of the default schema specified on the instance of the db context.

@rowanmiller
Copy link
Contributor

I've moved this one over to our docs repo, because we will put together documentation on how to do this. We haven't built out a full example of this yet... so it may drive specific improvements in the code base too.

@unlimitedcoders
Copy link

Did we get the answer to this?

@rmja
Copy link
Author

rmja commented Jan 30, 2016

No. Maybe @rowanmiller has an update?

@rowanmiller
Copy link
Contributor

Some discussion about how to do this in #4410

I'm working away at the list of docs at the moment, so I should have some proper docs on this in the next couple of weeks

@smbadiwe
Copy link

In case you landed here via search, I posted a simple working solution to this problem at dotnet/EntityFramework.Docs#96 (comment)

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Nov 20, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

6 participants