Base solution for your next web application
Open Closed

Adding IMustHaveTenant Breaks Initial DB Migration #3073


User avatar
0
mikemey01 created

Hi -

When adding IMustHaveTenant and TenantId field to an entity, then creating the migration, then running "dotnet ef database update" against a new database returns the error: "Can not set TenantId to 0 for IMustHaveTenant entities!"

If I run the database migrations without any entities utilizing IMustHaveTenant it works fine. If I then add the IMustHaveTenant to an entity and run the migrations it works fine. It's only when IMustHaveTenant is present when migrating for the first time to a new database instance. I have set the default in the migration for the TenantId column as below:

AddColumn("dbo.PhoneBookPersons", "TenantId", c => c.Int(nullable: false, defaultValue: 1));

Thanks,

Mike


3 Answer(s)
  • User Avatar
    0
    mikemey01 created

    Quick update - it looks like it had something to do with the seed data instead of the migration it self. I removed the seed data (the data that was provided in the development step-by-step guide) and it ran the migration flawlessly. I'm not sure why the seed data would be trying to insert 0 into the PbPersons.TenantId column but it was. I'll investigate further.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Since IMustHaveTenant adds a non nullable integer TenantId field to your entity, when you create a new entity in the seed method, the value of TenantId will be 0 if you don't set it. So, this is an expected behaviour, you need to set it expilicitly in the seed method.

  • User Avatar
    0
    mikemey01 created

    I think the issue I was running into is that it was inserting the tenantId of 1 into the PbPersons table from the seed class before the Default tenant was inserted into the AbpTenants table giving a FK reference error. I'm looking at the process order I setup now, either way seems to be fixed.

    Thanks,

    Mike