I wish to create seed data when a new tenant is created. What I am attempting to do is create a set of common records all tenants will start with. After that they can do what they want.
Do I run my seeding code when the DefaultTenantBuilder executes (which I assume runs ONLY when you create a new tenant) ?
If so do I get the newly created Tenant ID from ?
defaultTenant = new MultiTenancy.Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName);
Just to ensure I am not barking up the wrong tree, here is my code sample.
public void Create()
{
CreateDefaultTenant();
new InitialStateDataCreator(_context).Create();
}
............
class InitialStateDataCreator
{
readonly string[] stateArray = {"Queensland", "New South Wales", "Australian Capital Territory",
"Victoria", "Tasmania", "South Australia", "Western Australia",
"Northern Territory" };
private readonly CrazyCatLadyDemoDbContext _context;
public InitialStateDataCreator(CrazyCatLadyDemoDbContext context)
{
_context = context;
}
public void Create()
{
CreateStateData();
}
private void CreateStateData()
{
var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == MultiTenancy.Tenant.DefaultTenantName);
foreach (string state in stateArray)
{
var stateToFind = _context.States.FirstOrDefault(p => p.Name == state);
if (stateToFind == null)
{
_context.States.Add(
new State
{
Name = state,
TenantId = defaultTenant.Id
});
}
}
}
}