Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Multitenacy with DB per tenant #1584


User avatar
0
carelearning created

Hello,

We have been trying to set up an application using ASP.NET Zero Template where all users log on at 1 location. Then each tenant (more than 1 user per tenant) has their own database. We can log in as the global admin (no tenant ID) with no errors. However, when we try to login as a tenant user, we get:

Invalid object name 'dbo.AbpUsers'.

This table is only in the Host DB. The Tenant DB does not have this information. When we copy over this table from Host DB to Tenant DB then then we get more errors for multiple ABP* Tables. We want to authenticate against the Host DB, but not have all the abp tables duplicated in the tenant. Is this the correct approach?

For more information, here are the two Context classes:

public class TenantDbContext : AbpDbContext
    {
        public virtual IDbSet<Department> Departments { get; set; }

        public TenantDbContext() : 
            base("Tenant")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new DepartmentConfiguration());
        }
    }
public class HostDbContext : AbpZeroHostDbContext<Tenant, Role, User>
    {
        public virtual IDbSet<BinaryObject> BinaryObjects { get; set; }
        public virtual IDbSet<Friendship> Friendships { get; set; }
        public virtual IDbSet<ChatMessage> ChatMessages { get; set; }

        public HostDbContext()
            : base("Host")
        {
            
        }

        public HostDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }


        public HostDbContext(DbConnection dbConnection)
            : base(dbConnection, true)
        {

        }
    }

Thank you for your time and effort.


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I don't suggest to create 2 dbcontext since it adds complexity to database migrations and to the application (you can search for our answers for similar cases). We suggest to use single dbcontext approach we provided in the template as default. With the default template, all of these works out of the box.

    Even if you want to have 2 dbcontext, your TenantDbContext should be derived from AbpZeroTenantDbContext, not from AbpDbContext. You did it correct for host db context. Then you can run migrations to create needed tables in the tenant databases.

  • User Avatar
    0
    carelearning created

    Thank you for your reply. Here are the steps we used to resolve the issue we had:

    • We changed AbpDbContext to AbpZeroTenantDbContext<Role, User>
    • Added DefaultDbContext attribute to AbpZeroHostDbContext<Tenant, Role, User>
    • Seeded AbpLanguages with 1 record for English
    • Stopped seeding AbpUsers in the host context and instead created AbpUser for each TenantDbContext
    • MSDTC service was configured on DB Server and local dev machine and firewall rules had to be set

    Thank you again for your help and effort.