EF Migration fails with custom Identity user
See original GitHub issueHi, I followed the documentation to integrate the Multi-Tenancy for Identity server 4 with ASP.NET Core Identity
Also referred the sample for ASP.NET Core Identity
It works well if I use IdentityUser, but when I switch to a class extending the IdentityUser the migration fails to happen
Identity User -
[MultiTenant]
public class ApplicationUser : IdentityUser
{
public bool IsEnabled { get; set; }
public string TenantId { get; set; }
}
Identity context -
public class IdentityDbContext : MultiTenantIdentityDbContext<ApplicationUser>
{
public IdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
{
}
public IdentityDbContext(ITenantInfo tenantInfo, DbContextOptions<IdentityDbContext> options)
: base(tenantInfo, options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// builder.Entity<ApplicationUser>().IsMultiTenant();
}
Command I use for migration -
dotnet ef migrations add MultiTenant -c IdentityDbContext -o Data/Migrations/AspNetIdentity/AspNetIdentityDb
Error -
System.InvalidOperationException: The seed entity for entity type 'ApplicationUser' cannot be added because there was no value provided for the required property 'TenantId'.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateData(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.NpgsqlModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
Tried -
I tried putting [MultiTenant] over application user or
builder.Entity<ApplicationUser>().IsMultiTenant in Db Context s mentioned in documentation,
Both results in same error.
I tried defining TenantId in ApplicationUser same error 😦
If I put both ( [MultiTenant] and builder.Entity<ApplicationUser>().IsMultiTenant) I get below error -
**System.NullReferenceException: Object reference not set to an instance of an object.**
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.RemoveIndex(Index index)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.RemoveIndex(IMutableIndex index)
at Finbuckle.MultiTenant.EntityFrameworkCore.FinbuckleEntityTypeBuilderExtensions.RemoveIndex(EntityTypeBuilder builder, String propName)
at Finbuckle.MultiTenant.EntityFrameworkCore.FinbuckleEntityTypeBuilderExtensions.UpdateIdentityUserIndex(EntityTypeBuilder builder)
at Finbuckle.MultiTenant.EntityFrameworkCore.FinbuckleEntityTypeBuilderExtensions.IsMultiTenant[T](T builder)
Any help would be appreciated on how to get the ef migration working 😦
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (5 by maintainers)
That worked! Thank you very much Andrew
Thanks for the detail. I think I see the root cause.
I noticed here you probably want to inherit from
MultiTenantIdentityDbContext<ApplicationUser>
since you are replacing the existingIdenitityUser
implementation. You also don’t need to add aDbSet<ApplicationUser>
because when you use the generic base class it adds it for you. This works similarly for regular Identity when you use a custom user class.I think the way you have it now ends up with both a
IdentityUser
andApplicationUser
registered (the former is in the base class).Give that a try and let me know.
Also note that while you don’t have to add a
DbSet<ApplicationUser>
property you do still need to callUsers.IsMultiTenant()
inOnModeling
.