Home > database >  AspNetCore.Identity.IdentityUser to System.collection.Generic.ICollection
AspNetCore.Identity.IdentityUser to System.collection.Generic.ICollection

Time:11-27

I'm doing a hobby project for myself and later turn it into a portfolio work. I'm working in Entityframework 6, with razor pages.

My class UserModel is a child of the IdentityUser class. (I presume you know what an IdentityUser, if not) https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-7.0

Inside the UserModel I also stores an Icollection of GroupClass. My GroupClass has an ICollection of UserModels. Which creates a many to many relationship in EF6.

When i try to store the UserModel object in the groupclass, i get the error: Cannot implicitly convert type to AspNetCore.Identity.IdentityUser to System.collection.Generic.ICollection. What i understand is, i somehow need to change the class groupClass. To be able to store an identity variable instead of a Icollection usermodel. ` What i don't understand is, why isn't UserModel already a part of IdentityUser, as it's a child and should have the same parameters as it's parent.

My question is, how do i store the UserModel inside my groupclass?

public class GroupClassModel
    {
        [Key]
        public int GroupClassID { get; set; }
        //   info dump removed
        public string userID { get; set; }
        public ICollection<UserModel>? userModels { get; set; }

    }
public class UserModel : IdentityUser
    {

  
         // info dump removed
 
        public int? groupClassID { get; set; }
        public ICollection<GroupClassModel>? GroupClass{ get; set; }


     
    }

            if (ModelState.IsValid)
            {
              
                 //info removed
                if (newGroup == null)
                {
                    var NewGroup = new GroupClassModel()
                    {                     
                        userModels = await userManager.GetUserAsync(User)

                    };
                    Context.groupClass.Add(NewGroup );
                    Context.SaveChanges();

                }

                return RedirectToPage("Index");
            }
            return Page();
    public class Context : IdentityDbContext
    {
        Context(DbContextOptions<Context> options) : base(options)
        {
        }    
        public DbSet<GroupClassModel> groupClassModels { get; set; }
        public DbSet<UserModel> UserModels { get; set; }

        public DbSet<MasterModel> MasterModels { get; set; }

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

`

I tried changing the Icollection in both classes to Icollection. same problem. I tried changing Icollection To IdentityUser but i got tons of other errors. Now i'm 50/50 if latter is the correct way of doing this.

CodePudding user response:

GroupClassModel.userModels is a collection of UserModels. This line of code is trying to set that property to a single instance of a UserModel.

// this generates an error
userModels = await userManager.GetUserAsync(User);

Instead you need to instantiate a new collection and add the current user's UserModel to that collection. (You're also going to need to cast the return value of userManager.GetUserAsync, which is an IdentityUser, to UserModel.)

userModels = new List<UserModel>();
// currentUser is an IdentityUser
var currentUser = await userManager.GetUserAsync(User);
// cast the IdentityUser to a UserModel
UserModel currentUserModel = (UserModel)currentUser;
userModels.Add(currentUserModel);
  • Related