I have many to many relationship between computer and User. Unfortunately, there are not many examples of CRUD operations on many-to-many relationships. Most importantly adding and deleting. I tried like computer.ComputerUser.Add(), it works but deleting is confusing for me. I don't even know if I am doing it correctly. Your insights will be helpful. Thank you
// a computer can have one or more Users registered to access
public class Computer
{
[Key]
public Guid ComputerId { get; set; }
public string MachineName { get; set; }
public DateTime Updated { get; set; }
public bool IsActive { get; set; } = true;
public virtual ICollection<ComputerUser> ComputerUser { get; set; }
public Computer()
{
this.ComputerUser = new HashSet<ComputerUser>();
}
}
// an user can have access to one or more computers
public class User
{
[Key]
public Guid GatekeeperUserId { get; set; }
public string Name { get; set; }
public virtual ICollection<ComputerUser> ComputerUser { get; set; }
public GatekeeperUser()
{
this.ComputerUser= new HashSet<ComputerUser>();
}
}
// Joining table
[Key]
public Guid ComputerId { get; set; }
public Computer Computer { get; set; }
[Key]
public Guid UserId { get; set; }
public User User { get; set; }