title: insert data in many-to-many relationship in Entity Framework
body: <p>I have many-to-may relationship between two tables that breaks by introducing another table in between, containing primary keys of both.  </p>
  
  <p><img src="https://i.stack.imgur.com/3oufW.png" alt="enter image description here"></p>
  
  <p>and here is my model classes</p>
  
  <h2>Navigation_Functions</h2>
  
  <pre><code>public class Navigation_Functions
  {
      public Navigation_Functions()
      {
  
      }
      [Key]
      public int Function_ID { get; set; }
  
      [StringLength(250)]
      [Required(ErrorMessage = "Required Title")]
      [Display(Name = "Function Title")]
      public string FunctionName { get; set; }
  
      [Required(ErrorMessage = "Required Hierarchy Level")]
      [Display(Name = "Hierarchy Level")]
      public int Hierarchy_Level { get; set; }
  
      public ICollection&lt;Navigation_FunctionHierarchy&gt; Navigation_FunctionHierarchy { get; set; }
      public ICollection&lt;Navigation_FunctionInAction&gt; Navigation_FunctionInAction { get; set; }
      public ICollection&lt;Navigation_FunctionInController&gt; Navigation_FunctionInController { get; set; }
  }
  </code></pre>
  
  <h2>Navigation_FunctionController Model</h2>
  
  <pre><code> public class Navigation_FunctionController
  {
      public Navigation_FunctionController()
      {
  
      }
  
      [Key]
      public int ControllerID { get; set; }
  
      [StringLength(250)]
      [Required]
      public string ControllerName { get; set; }
  
      public ICollection&lt;Navigation_FunctionAction&gt; Navigation_FunctionAction { get; set; }
      public ICollection&lt;Navigation_FunctionInController&gt; Navigation_FunctionInController { get; set; }
  
  }
  </code></pre>
  
  <h2>Navigation_FunctionInController Model</h2>
  
  <pre><code>  public Navigation_FunctionInController() 
      { 
  
      }
  
      [Key]
      public int FunctionInController_ID { get; set; }
      public int Function_ID { get; set; }
      public int ControllerID { get; set; }
  
      public Navigation_FunctionController Navigation_FunctionController { get; set; }
      public Navigation_Functions Navigation_Functions { get; set; }
  
  }
  </code></pre>
  
  <p>I have generic repository for CRUD operation </p>
  
  <pre><code>public void InsertEntity(TEntity obj)
      {
          _DbSet.Add(obj);
      }
  </code></pre>
  
  <p>Now I have class responsible to insert data by calling generic repository and pass object with data, I am struggling to insert data as how I insert primary of both tables into Navigation_FunctionInController table, where Functions and Controller record are creating at same time. ??????????????????????????????????????</p>
  
  <p>I am reading blogs <a href="https://stackoverflow.com/questions/4253165/insert-update-many-to-many-entity-framework-how-do-i-do-it">Insert/Update Many to Many Entity Framework . How do I do it?</a> but I am struggling to understand how to insert data with my structure.</p>
  
  <h2>?????????</h2>
  
  <pre><code>    public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
      {
  
          using(var _uow = new FunctionsNavigation_UnitOfWork())
          {
              try
              {
                   ??????????????????????
  
              }
              catch
              {
  
              }
          }
      }
  </code></pre>
  
  <h2>My ViewModel</h2>
  
  <pre><code>public class FunctionsNavigation_ViewModel 
  {
      public Navigation_Functions _Navigation_Functions { get; set; }
  
      public Navigation_FunctionController _Navigation_FunctionController { get; set; }
  }
  </code></pre>
  
  <p>I have updated my model as been suggested but still failing to save data in database, following is screen shot from debug code.</p>
  
  <p><img src="https://i.stack.imgur.com/WB7cz.png" alt="enter image description here"></p>