I'd like to know how to access the Table from the Controller and View I suppose.
My models look like so
public class UserProfiles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Roles> Roles { get; set; } //many to many
public virtual ICollection<dbClient> Clients { get; set; } // many to many
}
My other model looks like
public class Roles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Required(ErrorMessage = "Required")]
public int RoleID { get; set; }
[Required(ErrorMessage = "Required")]
public string RoleName { get; set; }
public virtual ICollection<UserProfiles> UserProfiles { get; set; } //many to many
}
When i run migrations and update from the PMC ( Package Manager Console ), It brings the creates the 2 tables with Keys to a 3rd table that hold jsut the ID values of both Tables.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<OilNGasWeb.Models.Roles>()
.HasMany<OilNGasWeb.Models.UserProfiles>(r => r.UserProfiles)
.WithMany(u => u.Roles)
.Map(m =>
{
m.ToTable("Webpages_UsersInRoles");
m.MapLeftKey("RoleId");
m.MapRightKey("UserId");
});
}
Recently I have tried:
var result=(from u in db.UserProfiles
from r in u.Roles **1.**
where r.RoleID.Contains(u.UserId) **2.**
select u);
But it is somehow not picking up on Webpages_UsersInRoles... ( after it creates the table for me must I create a class for it???? )
Why can't I just link into it.
2Pics for @Andrew Counts
Steps i followed to get m2m tables
Generating the Many to Many EF Code First
Afterwards I tried to use with examples from :
All those and more I read through, but non say how to use a m2m from the controller level? to pass values into the View.
Thanks for any help