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

Get TenantId in seed method of TenantDbContext #2477


User avatar
0
carelearning created

Hello,

We are trying to get the tenant.Id in the Seed(AbpZeroTemplateTenantDbContext context) method. We are currently doing it as below:

protected override void Seed(AbpZeroTemplateTenantDbContext context)
{
//Data Source=(localdb)\projectsv13; Initial Catalog=abp_tenant_name;
var tenancyName = new SqlConnectionStringBuilder(context.Database.Connection.ConnectionString)
.InitialCatalog.TrimStart(CustomConsts.DatabasePrefix.ToCharArray());

// prefix: abp_ so tenant name is tenant_name
var hostContext = new AbpZeroTemplateDbContext();
var tenant = hostContext.Tenants.SingleOrDefault(t => t.TenancyName == tenancyName);

        var tenantId = 0;
        if (tenant != null)
            tenantId = tenant.Id;

        var initialTenantDbBuilder = new InitialTenantDbBuilder(context);
        initialTenantDbBuilder.Create(tenantId);

        context.SaveChanges();
    }

Is there a better or more simple way to do this?

Thank you for your time and effort.


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    When you are doing is fine. You need to query it from context.