Finbuckle / Finbuckle.MultiTenant

Finbuckle.MultiTenant is an open-source multitenancy middleware library for .NET. It enables tenant resolution, per-tenant app behavior, and per-tenant data isolation.
https://www.finbuckle.com/multitenant
Apache License 2.0
1.26k stars 261 forks source link

Null reference error on Add-Migration using MultiTenantIdentityDbContext #29

Closed Cpcrook closed 6 years ago

Cpcrook commented 6 years ago
System.NullReferenceException: Object reference not set to an instance of an object.
   at Finbuckle.MultiTenant.EntityFrameworkCore.Shared.SetupModel(ModelBuilder modelBuilder, TenantContext tenantContext) in C:\...\Finbuckle.MultiTenant\src\Finbuckle.MultiTenant.EntityFrameworkCore\Shared.cs:line 93
   at Finbuckle.MultiTenant.EntityFrameworkCore.MultiTenantIdentityDbContext`1.OnModelCreating(ModelBuilder builder) in C:\...\Finbuckle.MultiTenant\src\Finbuckle.MultiTenant.EntityFrameworkCore\MultiTenantIdentityDbContext.cs:line 74

Just looks like there needs to be some null-conditional checks added to allow migrations to apply happily.

var rightExp = Expression.Constant(tenantContext.Id, typeof(string));

should be

var rightExp = Expression.Constant(tenantContext?.Id, typeof(string));

I have a branch going I can submit a PR for (just need contributor permissions).

AndrewTriesToCode commented 6 years ago

Hi Chris, thanks for the feedback I'll give you contributor.

For design time db context creation I recommend using a design time factory: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-a-design-time-factory

In the factory class construct the db context using the constructor that doesn't require a tenant context--internally this creates a dummy tenant context which will allows the expression to work. add-migration and other cli tools will ignore the tenant context.