0
votes

ASP.Net Core 3 MVC web application with Identity

My DB context is

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {}
etc...

So Users, from IdentityUserContext is a public virtual DbSet, and is instantiated as

DbSet<Microsoft.AspNetCore.Identity.IdentityUser> Users

However, I have defined ApplicationUser : IdentityUser to define my own profile data.

public class ApplicationUser : IdentityUser
{
    [Display(Name = "Is Manager")]
    public bool IsSupervisor { get; set; }
    etc....

So, I have configured services in startup.cs as:

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)

I have set up my UserManager as

private readonly UserManager<ApplicationUser> _userManager

But when I try to use LINQ to access and filter User records based on my custom profile information (in the record 'Detail' call to the controler), I'm told that those custom attributes are not available:

        var managers = new List<SelectListItem>();
        managers.AddRange(_context.Users.Where(x => x.IsSupervisor == true).Select(manager => new SelectListItem
        {
            Text = manager.DisplayName,
            Value = manager.Id.ToString()
        }).ToList());

        ViewBag.ManagersList = managers;

/Users/robert/Projects/mvc/Vacate/Controllers/EmployeesController.cs(59,59): Error CS1061: 'IdentityUser' does not contain a definition for 'IsSupervisor' and no accessible extension method 'IsSupervisor' accepting a first argument of type 'IdentityUser' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Vacate)

So, without using a separate table to store profile information, is there a way to use LINQ against Users accessing your custom profile members? Sort of 'cast' Users to be the correct ApplicationUser type in the LINQ call? Or did I miss something when setting up the ApplicationUser class for use in Identity?

1

1 Answers

0
votes

You should make your default ApplicationDbContext use the new ApplicationUser :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

The update your database : Add-migration , Update-Database .