'How to use .NET Identity scaffold users in models

I have a model representing a project, a project is supposed to have one or more users attached to it. I have identity scaffolded in my project, and my applicationDB context inherits from the identityDBContext. I want to use the user "model" that comes with the scaffold in one of my own models. How can I accomplish this?

My application db context

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

       protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
           base.OnModelCreating(modelBuilder);
       }

       public DbSet<Project> projects { get; set; }
       public DbSet<Phase> phases { get; set; }
       public DbSet<Models.Task> tasks { get; set; }

   }

My project model

        public Project()
        {
            this.Users = new HashSet<???>();
        }
        [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public int Description { get; set; }

        public ICollection<???> Users { get; set; }
    }


Solution 1:[1]

Having seen between the line of your descriptions and code I would suggest follwing steps for you.

Let's begin with your statement "I want to use the user "model" that comes with the scaffold in one of my own models"

Inherit your IdentityUser class in Custom User Class:

public class User : IdentityUser 
    
    {
        public string NewCustomProperty { get; set; }
        public string AnotherCustomProperty { get; set; }

    }

Note: Remember that we are inheriting this for our implementation flexibility so that our base IdentityUser class doesn't get impacted. Therefore, Its not necessary that you have to have property in your derived User class.You can make it empty.

Then Inherit your IdentityDbContext class in your ApplicationDbContext:

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Project> projects { get; set; }
    public DbSet<Phase> phases { get; set; }
    public DbSet<Models.Task> tasks { get; set; }

}

Note: Make sure while you are Inheriting IdentityUser class make sure you have added the correct namespace which is using Microsoft.AspNetCore.Identity

Your Project Model Now Should be:

 public class Project
{
    public Project()
    {
        Users = new HashSet<User>();
    }
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
    public int Description { get; set; }

    public ICollection<User> Users { get; set; }
    
}

Note: I hope it will help you implement what you are trying to accomplish.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1