How to add many-to-many relationship to Identity AspNetUsers default table with code?

I am new to the Identity framework and the Code First method. I am currently using the default project when creating a new MVC5 project on VS 2015.

The default tables created when the project starts are great, but I want it to create a new one called tbSkills (Id, SkillName), along with others, and it should have many-to-many relationships with AspNetUsers.

So where and what should I write to accomplish this?

ps: I also changed the types of identifiers from string to int after the tutorial, it worked fine.

This is what I tried last, inside the ApplicationUser class, in the IdentityModels.cs file:

public class ApplicationUser : IdentityUser<int, MyUserLogin, MyUserRole, MyUserClaim>, IUser<int> { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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; } // // This following is my added code public ApplicationUser() { Skills = new HashSet<tbSkill>(); } public virtual ICollection<tbSkill> Skills { get; set; } } public class tbSkill { public tbSkill() { Users = new HashSet<ApplicationUser>(); } public int Id; public string SkillName; public virtual ICollection<ApplicationUser> Users { get; set; } } 

It ends with this error:

enter image description here

I already researched and read a bunch of articles, and was not successful.

+4
source share
1 answer

Try adding skill ID

 public class ApplicationUser : IdentityUser<int, MyUserLogin, MyUserRole, MyUserClaim>, IUser<int> { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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; } // // This following is my added code public ApplicationUser() { Skills = new HashSet<tbSkill>(); } public int SkillId {get;set;} public virtual ICollection<tbSkill> Skills { get; set; } } 
0
source

All Articles