I am trying to map a one-to-many relationship between users & groups. I want the users to have an individual ranking for each group they join, relative only to that group. Unfortunately EF will not map what I want, and I've been stuck trying to figure out why for 2 days.
IdentityModels.cs:
namespace MapGroupsToRoles.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public ApplicationUser() { this.RolesToGroups = new HashSet<GroupToRoles>(); }
public virtual ICollection<GroupToRoles> RolesToGroups { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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 class GroupToRoles
{
public virtual int GroupToRolesId { get; set; }
public GroupToRoles() { }
public virtual ApplicationUser User { get; set; }
public virtual Group Group { get; set; }
public virtual ClanRole Role { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public System.Data.Entity.DbSet<GroupToRoles> GroupToRolesTable { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
modelbuilder.Entity<ApplicationUser>().ToTable("Users");
// modelbuilder.Entity<ApplicationUser>().Ignore(u => u.Id);
modelbuilder.Entity<ApplicationUser>().HasKey(u => u.Id);
modelbuilder.Entity<ApplicationUser>().HasMany(u => u.RolesToGroups).WithRequired().WillCascadeOnDelete(true);
modelbuilder.Entity<GroupToRoles>().ToTable("GroupToRolesTable").HasKey(u => u.GroupToRolesId);
modelbuilder.Entity<IdentityUserLogin>().HasKey(u => u.ProviderKey);
modelbuilder.Entity<IdentityUserRole>().HasKey(u => u.RoleId);
modelbuilder.Entity<Group>().ToTable("Groups");
modelbuilder.Entity<Group>().HasKey(u => u.GroupId);
modelbuilder.Entity<Group>().HasMany(u => u.Roles).WithRequired().WillCascadeOnDelete(true);
}
}
}
Above: the important parts to note here are:
public class GroupToRoles
{
public virtual int GroupToRolesId { get; set; }
public GroupToRoles() { }
public virtual ApplicationUser User { get; set; }
public virtual Group Group { get; set; }
public virtual ClanRole Role { get; set; }
}
and
public virtual ICollection<GroupToRoles> RolesToGroups { get; set; }
inside the ApplicationUser class.
Group.cs:
namespace MapGroupsToRoles.Models
{
public class Group
{
public int GroupId { get; set; }
public Group() { this.Roles = new HashSet<GroupToRoles>(); }
public virtual string GroupName { get; set; }
public virtual ICollection<GroupToRoles> Roles { get; set; }
}
}
Right, so what results am I getting? 1. Both Hashsets (ie the GroupToRoles sets in Group.cs and ApplicationUser do not appear... 2. The group table displays Group_GroupId1 and Group_GroupId ... where's the 1 come from?!
Any help would be hugely appreciated!!!!
EDIT_____________
So re-iterate I want to map users to groups, where they have one role per group, and be a member of many groups. Therefore it is a map of one to many between user and "GroupToRoles" class (which in turn maps one to many groups).
Example: John is a member of groups a,b,c for group a he is admin, group b he is a member group c he is a visitor
I don't think I can use (please correct me if im wrong) the standard ASP roles (which is why i created the "ClanRole" class, as I also want a single global role per user which will handle admin rights on the entire site (not just groups the uses have created).
Basically, I want to be able to map the ApplicationUser class to many "GroupToRoles". From there, everything else should fall into place. It is however resisting and I can't work out why!
Many thanks