title: Can I assign many-to-many relations with ID only?
body: <p>I'm building a web api with asp.net mvc core and entityframework.</p>
  
  <p>I have two objects with a many-to-many relationship between them.</p>
  
  <pre><code>class User
  {
      public string ID { get; set; }
      public List&lt;Message&gt; Inbox { get; set; }
  }
  
  class Message
  {
      public int ID { get; set; }
      public List&lt;User&gt; Recipients { get; set; }
  }
  </code></pre>
  
  <p>To create a new message, the client sends a JSON to my controller, with the following structure:</p>
  
  <pre><code>// POST /api/message
  {
      body: "...", // the body and other properties of the message
      recipients: ["user1", "user2"] // a list of the IDs of the recipients of this message
  }
  </code></pre>
  
  <p>So basically I need to create a new <code>Message</code> object and assign its <code>Recipients</code> property based on the user IDs that I received in the JSON.</p>
  
  <p>So far I'm pulling each <code>User</code> object from the database to assign it to my <code>Message</code>, but this looks like a waste of resources.</p>
  
  <pre><code>foreach (var userId in json.recipients)
  {
      var userObject = _dbContext.Users.Find(userId);
      message.Recipients.Add(userObject)
  }
  </code></pre>
  
  <p>There must be a better way, right? How can I assign the recipients without pulling all that stuff from the database? Or at the very least by doing only one request?</p>