question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Migration fails with custom Identity user

See original GitHub issue

Hi, I followed the documentation to integrate the Multi-Tenancy for Identity server 4 with ASP.NET Core Identity

https://www.finbuckle.com/MultiTenant/Docs/Identity

Also referred the sample for ASP.NET Core Identity

https://github.com/Finbuckle/Finbuckle.MultiTenant/tree/master/samples/ASP.NET Core 3/IdentityDataIsolationSample

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:closed
  • Created 3 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
goforebrokecommented, Nov 20, 2020

That worked! Thank you very much Andrew

1reaction
AndrewTriesToCodecommented, Nov 20, 2020

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 existing IdenitityUser implementation. You also don’t need to add a DbSet<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 and ApplicationUser registered (the former is in the base class).

public class ApplicationDbContext : MultiTenantIdentityDbContext

// change to
public class ApplicationDbContext : MultiTenantIdentityDbContext<ApplicationUser>

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 call Users.IsMultiTenant() in OnModeling.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Unable to create migration in EF Core 6 using custom ...
The problem is whenever I try to create the intial migration, I get the following error: The entity type 'RoleClaims' requires a primary...
Read more >
EF Core 2.0 migration issue using Identity #9597
I'm taking a ASP.NET Core 1.1 project and upgrading to Core 2.0. It went well enough with an existing DB, but when I...
Read more >
Identity model customization in ASP.NET Core
This article describes how to customize the underlying Entity Framework Core data model for ASP.NET Core Identity.
Read more >
Migrations and Seed Data With Entity Framework Core
We are going to learn about Migrations and Seed data in Entity Framework Core and how to optimize EF Core migrations.
Read more >
Entity Framework Core Migrations
To use migrations in EF Core, you start by defining your database schema using code, such as POCO classes and DbContext .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found