MultiTenant Shared Schema

Are there any samples of creating a MultiTenant application where the database is shared and the URL remains the same?

Hi @Glenn_Tosco,

We have this sample which uses multiple databases and determines the current tenant based on the URL.

You can probably support single database and URL if you keep a TenantID or similar property per user. This help article shows how to extend the ApplicationUser class with a new property.

Hi @Glenn_Tosco,
Do you need a MultiTenant application?.
If you need to restrict data in certain sql tables based on the logged-in user it is simple using a custom context and a server side filter similar. E.G

  namespace FilterDataRecordsByUser.Data
{
    public partial class FilterByUserContext : Microsoft.EntityFrameworkCore.DbContext
    {       
        public string UserId { get; set; }
        partial void OnModelBuilding(ModelBuilder builder)
        {
            builder.Entity< FilterDataRecordsByUser.Models.FilterByUser.FilterByUser>().HasQueryFilter(b => EF.Property<string>(b, "UserId") == UserId);
        }
                
        public override int SaveChanges()
        {
            ChangeTracker.DetectChanges();

            foreach (var item in ChangeTracker.Entries().Where(
                e =>
                    e.State == EntityState.Added && e.Metadata.GetProperties().Any(p => p.Name == "UserId")))
            {
                item.CurrentValues["UserId"] = UserId;
            }
            return base.SaveChanges();
        }
    }
}
1 Like

I've built several multi-tenant apps...

I've built them with the TenantId or ApplicationId in the tables and used one database...

I've built them with a database for each Tenant or Application...

For my business, it is much easier to manage resources, migrate and bill when the database is separate.

It also makes it easier for a Tenant or Application to be moved to a faster server on Everleap. I can simply move the app and database from a shared cloud to a dedicated cloud. There is no "extra" work exporting and importing the data.

1 Like

Thanks for the insight, we are reviewing the options to see what is the best solution for us.