Open Closed

Issue Seeding default entries to tenant DB when new tenant is created #2041


User avatar
0
manojkumar.t@shloklabs.com created
  • ABP Framework version: vX.X.X
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

Our Problem

I am trying to seed some default records into the tenant side database, whenever a new tenant is created.

Normally, while creating a tenant editionDataSeeder in the SaasDataSeeder is triggered. I would like to create such a functionality to create set of DB with some default entries.

What I have done?

Step 1:

Created a new entity with FullAuditedAggregateRoot and IMulititenant

public class Enterprise : FullAuditedAggregateRoot<Guid>, IMultiTenant
 {
     public virtual Guid? TenantId { get; set; }

     [NotNull]
     public virtual string EnterpriseName { get; set; }

     [NotNull]
     public virtual string EnterpriseHostName { get; set; }

     [NotNull]
     public virtual string EnterpriseEmail { get; set; }

     public Enterprise(Guid id, string enterpriseName, string enterpriseHostName, string enterpriseEmail, Guid? tenantId)
     {
         Id = id;
         Check.NotNull(enterpriseName, nameof(enterpriseName));
         Check.Length(enterpriseName, nameof(enterpriseName), EnterpriseConsts.EnterpriseNameMaxLength, EnterpriseConsts.EnterpriseNameMinLength);
         Check.NotNull(enterpriseHostName, nameof(enterpriseHostName));
         Check.Length(enterpriseHostName, nameof(enterpriseHostName), EnterpriseConsts.EnterpriseHostNameMaxLength,   EnterpriseConsts.EnterpriseHostNameMinLength);
         Check.NotNull(enterpriseEmail, nameof(enterpriseEmail));
         Check.Length(enterpriseEmail, nameof(enterpriseEmail), EnterpriseConsts.EnterpriseEmailMaxLength, EnterpriseConsts.EnterpriseEmailMinLength);
         EnterpriseName = enterpriseName;
         EnterpriseHostName = enterpriseHostName;
         EnterpriseEmail = enterpriseEmail;
         TenantId = tenantId;
     }
 }

Step 2:

Created a Dataseeder class for some default entries.

public class EnterpriseDataSeedContributor : IDataSeedContributor, ITransientDependency
    {

        private readonly IEnterpriseRepository _enterpriseRepository;
        private readonly IGuidGenerator _guidGenerator;
        private readonly ICurrentTenant _currentTenant;

        public EnterpriseDataSeedContributor(
            IEnterpriseRepository enterpriseRepository,
            IGuidGenerator guidGenerator,
            ICurrentTenant currentTenant
            )
        {
            _enterpriseRepository = enterpriseRepository;
            _guidGenerator = guidGenerator;
            _currentTenant = currentTenant;
        }

        [UnitOfWork]
        public virtual async Task SeedAsync(DataSeedContext context)
        {
            using (_currentTenant.Change(context?.TenantId))
            {
                if (await _enterpriseRepository.GetCountAsync() > 0)
                {
                    return;
                }

                var enterprise = new Enterprise(
                    id: _guidGenerator.Create(),
                    enterpriseName: "Initial Seed",
                    enterpriseHostName: "initial",
                    enterpriseEmail: "initailSeed@gmail.com",
                    tenantId: context?.TenantId
                );

                await _enterpriseRepository.InsertAsync(enterprise);
            }
        }
    }

I believe that this unit of work will automatically get executed from TenantDatabaseMigrationHandler inside the Domain/Data .

Step 3:

Running the projects Host, Identity service and Web. Now I am trying to create a new tenant with seperate tenant database.

Result:

Tenant database is created with all the default tables, but the table respective to the above entity (Enterprise) is not created.

I had followed this article, https://docs.abp.io/en/abp/latest/Multi-Tenancy#imultitenant

Default tables are created,

In the Host Database, the Enterprise table is there.

Can you guys help us here?


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Does your project have EntityFrameworkCore.DbMigrations?

    https://community.abp.io/articles/unifying-dbcontexts-for-ef-core-removing-the-ef-core-migrations-project-nsyhrtna

  • User Avatar
    0
    manojkumar.t@shloklabs.com created

    No, we don't have that project,

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Your EF Core has two DbContext right?

    Can you share the detaisl? How do you configure your entity?

  • User Avatar
    0
    manojkumar.t@shloklabs.com created

    Hi, We had fixed it from our end.

    Thanks

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11