Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access Table built by MVC EF automatically -m2m
    primarykey
    data
    text
    <p>I'd like to know how to access the Table from the Controller and View I suppose.</p> <p>My models look like so </p> <pre><code>public class UserProfiles { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string UserName { get; set; } public virtual ICollection&lt;Roles&gt; Roles { get; set; } //many to many public virtual ICollection&lt;dbClient&gt; Clients { get; set; } // many to many } </code></pre> <p>My other model looks like</p> <pre><code>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&lt;UserProfiles&gt; UserProfiles { get; set; } //many to many } </code></pre> <p><strong>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.</strong></p> <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;OilNGasWeb.Models.Roles&gt;() .HasMany&lt;OilNGasWeb.Models.UserProfiles&gt;(r =&gt; r.UserProfiles) .WithMany(u =&gt; u.Roles) .Map(m =&gt; { m.ToTable("Webpages_UsersInRoles"); m.MapLeftKey("RoleId"); m.MapRightKey("UserId"); }); } </code></pre> <p><img src="https://i.stack.imgur.com/TSh7p.png" alt="enter image description here"><img src="https://i.stack.imgur.com/h4KYV.png" alt="enter image description here"></p> <p>Recently I have tried:</p> <pre><code>var result=(from u in db.UserProfiles from r in u.Roles **1.** where r.RoleID.Contains(u.UserId) **2.** select u); </code></pre> <p>But it is somehow not picking up on Webpages_UsersInRoles... ( after it creates the table for me must I create a class for it???? ) </p> <p>Why can't I just link into it.</p> <hr> <p><strong>2Pics for @Andrew Counts</strong></p> <p><img src="https://i.stack.imgur.com/QRbzC.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/qmPsX.png" alt="enter image description here"></p> <hr> <p>Steps i followed to get m2m tables</p> <p><a href="http://www.codeproject.com/Tips/548945/Generating-Many-to-Many-Relation-in-MVC4-using-Ent" rel="nofollow noreferrer">Generating the Many to Many EF Code First</a> </p> <p>Afterwards I tried to use with examples from :</p> <p><a href="https://stackoverflow.com/questions/15662145/entity-framework-code-first-many-to-many-relationship-and-inheritance">Getting m2m work with view</a></p> <p><a href="https://stackoverflow.com/questions/15662145/entity-framework-code-first-many-to-many-relationship-and-inheritance">m2m </a></p> <p><a href="https://stackoverflow.com/questions/17443221/mvc4-controller-with-many-to-many-relationship">m2m controller updating</a></p> <p>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.</p> <p>Thanks for any help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload