Adding Tenants Table and TenantId Field

We need to add a TenantId field to all tables, to isolate tenants from each other.

So, we first need a Tenants table.

As Northwind tables already have records, we’ll define a primary tenant with ID 1, and set all existing records TenantId to it.

It’s time to write a migration, actually two migrations, one for Northwind and one for Default database.

DefaultDB_20170430_134800_MultiTenant.cs:

  1. using FluentMigrator;
  2. namespace MultiTenancy.Migrations.DefaultDB
  3. {
  4. [Migration(20170430134800)]
  5. public class DefaultDB_20170430_134800_MultiTenant
  6. : AutoReversingMigration
  7. {
  8. public override void Up()
  9. {
  10. this.CreateTableWithId32("Tenants", "TenantId", s => s
  11. .WithColumn("TenantName").AsString(100)
  12. .NotNullable());
  13. Insert.IntoTable("Tenants")
  14. .Row(new
  15. {
  16. TenantName = "Primary Tenant"
  17. });
  18. Insert.IntoTable("Tenants")
  19. .Row(new
  20. {
  21. TenantName = "Second Tenant"
  22. });
  23. Insert.IntoTable("Tenants")
  24. .Row(new
  25. {
  26. TenantName = "Third Tenant"
  27. });
  28. Alter.Table("Users")
  29. .AddColumn("TenantId").AsInt32()
  30. .NotNullable().WithDefaultValue(1);
  31. Alter.Table("Roles")
  32. .AddColumn("TenantId").AsInt32()
  33. .NotNullable().WithDefaultValue(1);
  34. Alter.Table("Languages")
  35. .AddColumn("TenantId").AsInt32()
  36. .NotNullable().WithDefaultValue(1);
  37. }
  38. }
  39. }

I have created Tenants table in Default database where user tables are. Here we add 3 predefined tenants. We actually only need first one with ID 1.

We didn’t add TenantId column to tables like UserPermissions, UserRoles, RolePermissions etc, as they instrinsicly have TenantId information through their UserId or RoleId (as these tables already have TenantId value)

Let’s write another migration for Nortwhind database to add TenantId column to required tables:

NorthwindDB_20160110_093500_MultiTenant.cs:

  1. using FluentMigrator;
  2. namespace MultiTenancy.Migrations.NorthwindDB
  3. {
  4. [Migration(20170430194100)]
  5. public class NorthwindDB_20170430_194100_MultiTenant
  6. : AutoReversingMigration
  7. {
  8. public override void Up()
  9. {
  10. Alter.Table("Employees")
  11. .AddColumn("TenantId").AsInt32()
  12. .NotNullable().WithDefaultValue(1);
  13. Alter.Table("Categories")
  14. .AddColumn("TenantId").AsInt32()
  15. .NotNullable().WithDefaultValue(1);
  16. Alter.Table("Customers")
  17. .AddColumn("TenantId").AsInt32()
  18. .NotNullable().WithDefaultValue(1);
  19. Alter.Table("Shippers")
  20. .AddColumn("TenantId").AsInt32()
  21. .NotNullable().WithDefaultValue(1);
  22. Alter.Table("Suppliers")
  23. .AddColumn("TenantId").AsInt32()
  24. .NotNullable().WithDefaultValue(1);
  25. Alter.Table("Orders")
  26. .AddColumn("TenantId").AsInt32()
  27. .NotNullable().WithDefaultValue(1);
  28. Alter.Table("Products")
  29. .AddColumn("TenantId").AsInt32()
  30. .NotNullable().WithDefaultValue(1);
  31. Alter.Table("Region")
  32. .AddColumn("TenantId").AsInt32()
  33. .NotNullable().WithDefaultValue(1);
  34. Alter.Table("Territories")
  35. .AddColumn("TenantId").AsInt32()
  36. .NotNullable().WithDefaultValue(1);
  37. }
  38. }
  39. }