Search code examples
c#asp.net-coreentity-framework-coreasp.net-core-identity

How to Map Discriminator Column when base type is IdentittyUser?


I customized IdentityUser and named it User. Now extending User as Doctor and Secretary.

using Microsoft.AspNetCore.Identity;
//...

public class User : IdentityUser
{
    public string UserType {get; set;}
    //some props
}

public class Doctor : User
{
    //some props
}

public class Secretary : User
{
    //some props
}

//...

I know that EF Core will create a Discriminator as shadow property but I want to map this Discriminator to User.UserType property. So here is my configuration on DbContext:

builder.Entity<User>()
            .HasDiscriminator(u=>u.UserType)
            .HasValue<User>("user")
            .HasValue<Doctor>("doctor")
            .HasValue<Secretary>("secretary");

I got this error when adding migration:

The CLR property 'UserType' cannot be added to entity type 'IdentityUser' because it is declared on the CLR type 'User'.

Why does this error happen? Is this the nice way I go?

Do you know any proper way of making different types of users?


Solution

  • I just changed

    MyDbContext : IdentityDbContext {...}
    

    to

    MyDbContext : IdentityDbContext<User> {...}
    

    And the problem resolved.