Search code examples
c#mysqlentity-frameworkasp.net-mvc-5asp.net-identity

Extra column on user-roles table when using custom identity user(ASP.NET 5.2EF 6 and Identity 2)


I'm using a 4-layer based architecture for my MVC app : Domain,Data,Service and MVC. In my domain layer , i've created an Employee class inheriting from IdentityUser. It contain custom properties and the GenerateUserIdentityAsync method i've got from the application user class in identitymodes in MVC.

public class Employee : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Employee> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public String CIN { get; set; }
    public String FirstName { get; set; }

In my data layer, i've created a context inheriting from IdentityDbContext . I don't mention employee in my dbsets. I've also added numerous regarding my application and especially the employee class.

public class MyCWCContexte : IdentityDbContext<Employee>
{
    public MyCWCContexte()
        : base("name=CWCDB")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

The last change i've done is that i've changed the StartUp.Auth.cs and IdentityConfig files so they match my Employee class and my context.

 public class ApplicationUserManager : UserManager<CWC.Domain.Entities.Employee>
{
    public ApplicationUserManager(IUserStore<CWC.Domain.Entities.Employee> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore<CWC.Domain.Entities.Employee>(context.Get<MyCWCContexte>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<CWC.Domain.Entities.Employee>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

Generating the database from migrations goes well, the problem is with aspnet auto-generated tables, from IdentityUserRole class for example. When trying to add an user and adding a role to it, i've seen that aspnetuserroles contains 3 columns as stated here :

Screenshot of select * from aspnetuserroles UserId and RoleId are the PKs of the table, and Employee_Id is a FK from Employee. I think that UserId should be the Fk instead. It's really a problem since Authorizate annotation and GetRoles from usermanager for example don't work well. UserManager.GetRoles(Employee.Id) always returns null and the Employee.Id I get has the correct value i've got from database. I think it's because the comparaison is made on the employee_id instead of the userid. Do you have an idea about this ?I'm a beginner in ASP.NET. Thank you in advance.


Solution

  • Corrected it with removing the genericity(Employee) from DbContext : IdentityDbContext My context now is : public class MyCWCContexte : IdentityDbContext