2

I'm working on multitenant application (ASP.NET 5 + EF7). Each tenant will have separate database. I will have one separate database for tenant account data. I have registered service for EF in startup class for this separate database. I have problem with migrations. I cant create EF migration, until tenantDbContext is registered as service with specific connection string. But this conection string must be dynamic for each tenant... Any idea please? What is the best option to manage DbContexts for tenants?

Future edit - protected override void OnConfiguring was the key how to do: Is this good solution please?

 services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));


services.AddEntityFramework()
     .AddSqlServer()
            .AddDbContext<TenantDbContext>();  




public class TenantDbContext : IdentityDbContext<ApplicationUser>
{
    public TenantDbContext()        //development database with no connectionString in constructor
    {
        this._connectionString = "Connection String";
    }
    public TenantDbContext(string ConnectionString)
    {
        this._connectionString = ConnectionString;
    }

    private string _connectionString { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    { 
        optionsBuilder.UseSqlServer(_connectionString); 
    }  

...etc

3
  • My initial thought is instead of wiring up a context at startup, get you context from a factory. Something like: ContextFactory.Create(tenant). For migrations you could maybe use: Add-Migration MyMigration -ConnectionString "connection string here;" ? Commented Oct 20, 2015 at 7:12
  • Thanks for reply. Provisioning of new tenants must be automated - C#... IDEA: I can instantiate DbContext with specific conection string in constructor for connection. Question is how to create database without migrations (I cant create migration until is service registered in startup class) I will not use SQL script... Commented Oct 20, 2015 at 7:19
  • Maybe second idea. Is possible create database without migrations via database initializers? If yes, how to manage database changes in future? Commented Oct 20, 2015 at 7:29

1 Answer 1

2

As I mentioned in comments I have not tried multi-tenant/multi-db myself but try the following:

You can use DbContext CreateIfNotExists() method. https://msdn.microsoft.com/en-us/library/system.data.entity.database.createifnotexists(v=vs.113).aspx

If you have a Migrations/Configuration.cs you can set AutomaticMigrationsEnabled property to false

Setting the initializer off is probably needed as well: Database.SetInitializer<DatabaseContext>(null);

Sorry without knowing more details like workflow of creating a new tenant (automatic from DB or is a screen filled out with the connection string and name etc.) I can't make more detailed suggestions. I would suggest that your data layer be quite abstracted from the context. It seems like a bad idea for developers to have to select the correct context. Hence the use of a factory.

An option is always requiring a tenant id to be passed into all service or repository methods. I'm guessing this would be in some kind of user claim available in the controller.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. Great Idea.. I have now possibility to construct tenantDbContext. I foundded EF7 database initializers here: https://github.com/aspnet/EntityFramework/wiki/Entity-Framework-Design-Meeting-Notes---May-29,-2014 Each tenantDbContext can be instantiated by factory base on url: tenant1.site.com and injected to generic repository for specific tenant database operation. I will use one instance per request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.