title: In EF Core 5, how can I insert an entity with a many to many relation by setting only the foreigns keys IDs, without querying first?
body: <p>The other table contain references data with well know ID. The use case is to read data from file, create entities then insert them in batch. I don't need to query anything first, so all entities are &quot;disconnected&quot; from context.</p>
  <p>Simple exemple:</p>
  <pre><code>public class Post
  {
      public int ID { get; set; }
      public string Text { get; set; }
      public virtual ICollection&lt;Tag&gt; Tags { get; set; } 
  }
  
  public class Tag
  {
      public int ID { get; set; }
      [Required]
      public string Label { get; set;}
      public virtual ICollection&lt;Post&gt; Posts { get; set; } 
  }
  </code></pre>
  <p><strong>First try</strong></p>
  <pre><code>List&lt;Post&gt; posts = new List&lt;Post&gt;();
  
  loop
    var post = new Post { Text = &quot;some text&quot;});
    post.Tags.Add(new Tag {ID = 1});  
    post.Tags.Add(new Tag {ID = 2});
    posts.Add(post);
  ...
  
  context.Posts.AddRange(posts);
  context.SaveChange();
  </code></pre>
  <p>Error because EF try to update the tags record by setting the other tag column to null. I don't want EF to update the tag table anyway, only the join table.</p>
  <p><strong>Second try</strong><br />
  After reading <a href="https://stackoverflow.com/a/43700648/5410589">Long story short: Use Foreign key and it will save your day</a>, I did not find a way to make it work with a collection of FKs because in my case it's a many to many relation.</p>
  <p><strong>Third try</strong><br />
  Instead of using <code>context.Post.AddRange(posts);</code>, I attach only the parent entity:</p>
  <pre><code>var post = new Post { Text = &quot;some text&quot;});
  post.Tags.Add(new Tag {ID = 1});
  post.Tags.Add(new Tag {ID = 2});
  context.Posts.Attach(post).State = EntityState.Added;
  context.SaveChanges();
  </code></pre>
  <p>That worked. Post is inserted, and the joining table PostsTags contain the relation data, with the Tags table left untouched.
  <strong>BUT</strong> that will not work in batch (same context) because I can't then create another post with the same tag. The context &quot;track&quot; the tags by their ID, so I can't insert a &quot;new&quot; one with the same ID.</p>
  <p><strong>Fourth try</strong><br />
  What I'm doing right now is instead of adding a new Tag <code>post.Tags.Add(new Tag {ID = 1});</code>, I add the tag from the db <code>post.Tags.Add(context.Tags.Find(1));</code> That means many trips to database, for information that is already knows.</p>
  <p>Others options I think of is to keep a local dictionnary of tag that are already attached to context, change context between each post, find a way to insert data directly into the entity type that represent the join table, query all references beforehand (but some references tables contains thousand of elements) or simply juste use raw sql query.</p>
  <p>I can't imagine that there is no simple way to insert a model with Fk ids, like it work for a one to many by using a Foreign Key  property.</p>
  <p>Thank you</p>