title: EntityFramework - Updating many-to-many relationships
body: <p>I'm trying to update a many-to-many relationship. I have two models, diary and tags. The model diary contains a list of tags and tags contains a list of diaries. But whenever I try to add another tag to an existing list or change an existing one I get thrown an exception: </p>
  
  <p><strong>The entity type List`1 is not part of the model for the current context.</strong></p>
  
  <p>Does my way of updating even work on collections? Or is there another approach I should be looking at?</p>
  
  <p>Diary Model</p>
  
  <pre><code>public class Diary
  {
      [Key]
      public int IdDiary { get; set; }
  
      [Required(ErrorMessage = "This field is required")]
      public string NameDiary { get; set; }
  
      [Required(ErrorMessage = "This field is required")]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
      public DateTime CreationDate { get; set; }
  
      public bool Locked { get; set; }
  
      public ICollection&lt;Entry&gt; Entries { get; set; }
  
      [DataType(DataType.MultilineText)]
      [Required(ErrorMessage = "This field is required")]
      public string Summary { get; set; }
  
      public string ImageUrl { get; set; }
  
      public ICollection&lt;Tag&gt; Tags { get; set; }
  }
  </code></pre>
  
  <p>Entry Model</p>
  
  <pre><code>public class Entry
  {
      [Key]
      public int IdEntry { get; set; }
  
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
      public DateTime CreationDate { get; set; }
  
      [DataType(DataType.MultilineText)]
      public string EntryText { get; set; }
  
      [ForeignKey("Diary_IdDiary")]
      public Diary Diary { get; set; }
  
      public int Diary_IdDiary { get; set; }
      public string EntryName { get; set; }
  }
  </code></pre>
  
  <p>Tag Model</p>
  
  <pre><code>public class Tag
  {
      [Key]
      public int Id { get; set; }
      public string Name { get; set; }
      public ICollection&lt;Diary&gt; Diaries { get; set; }
  }
  </code></pre>
  
  <p>Update Method</p>
  
  <pre><code>    public void UpdateDiary(Diary updatedDiary)
      {
          var searchResult = SearchDiary(updatedDiary);
          if (searchResult != null)
          {
              updatedDiary.IdDiary = searchResult.IdDiary;
              _context.Entry(searchResult).CurrentValues.SetValues(updatedDiary);
              _context.Entry(searchResult.Tags).CurrentValues.SetValues(updatedDiary.Tags);
              _context.SaveChanges();
          }
      }
  </code></pre>
  
  <p>SearchDiary Method</p>
  
  <pre><code>    public Diary SearchDiary(Diary searchDiary)
      {
          var queryResult =
              _context.Diaries.Include(d =&gt; d.Entries).Include(d =&gt; d.Tags)
                  .Where(d =&gt; (d.NameDiary == searchDiary.NameDiary &amp;&amp; d.CreationDate == searchDiary.CreationDate) || d.IdDiary == searchDiary.IdDiary);
          return queryResult.FirstOrDefault();
      }
  </code></pre>
  
  <p>Thank you for reading</p>