Open Closed

Issue with separate host and tenant databases #1981


0
balessi75 created
  • ABP Framework version: 4.4.3
  • UI type: Blazor Server
  • DB provider: EF Core

We've created a solution using abp suite and chose to separate host and tenant db's. Each tenant is configured to have its own separate database. Everything is working as expected using the base abp modules when logging in and out of the host and tenant databases.

The problem we are running into is that, when we create a new entity, the entity is always being accessed from the host database, even when we are logged into a specific tenant database.

There are two database contexts that were created from the project template (AppNameDbContext and AppNameTenantDbContext). Our entity’s custom repository is referencing the TenantDbContext as follows:

        public class EfCorePayPeriodRepository : EfCoreRepository<TimepieceTenantDbContext, PayPeriod, int>, IPayPeriodRepository{...}

We also made sure to include the following in the ConfigureServices method of the EnittyFrameworkCore Module:

        context.Services.AddAbpDbContext<TimepieceTenantDbContext>(options =>             {                 options.AddDefaultRepositories(includeAllEntities: true);                 options.AddRepository<PayPeriod, EfCorePayPeriodRepository>();             });

The DbSet and Model building was also done in the TenantDbContext.

When using the repository, however, the framework always attempts to access the host database instead of the tenant database. Is there something we are missing with regards to entities that should only be created and accessed from the tenant database?

Guidance is much appreciated. Thanks.


2 Answer(s)
  • 0
    albert created
    Support Team

    and did you implement the IMultiTenant interface for your new entities. https://docs.abp.io/en/abp/latest/Multi-Tenancy#imultitenant

  • 0
    balessi75 created

    and did you implement the IMultiTenant interface for your new entities. https://docs.abp.io/en/abp/latest/Multi-Tenancy#imultitenant

    Thank you albert. Implementing IMultiTenant resolved the issue. We had thought that since every tenant will have it's own separate tenant database, a TenantId column was not needed and therefore IMultiTenant would not need to be implemented.