title: How to "inflate" entity with AutoMapper
body: <p>I'm receiving an DTO in my web api project and I'd like to use AutoMapper to automagically convert my DTO to the entity which I'm inserting into the database.</p>
  
  <p>Here is a simplification of the DTO and the entities:</p>
  
  <pre><code>class RegistrationDTO
  {
      string name;
      ICollection&lt;int&gt; Departments;
  }
  
  class Registration
  {
      int id;
      DateTime CreatedAt;
      string name;
      virtual ICollection&lt;Department&gt; Departments;
  }
  
  class Department
  {
      int id;
      string name;
      virtual ICollection&lt;Registration&gt; Registrations;
  }
  </code></pre>
  
  <p>The problem is that RegistrationDTO only has the ids of the departments and I can't find a way to get AutoMapper to get the departments from the database (using Entity Framework 5).</p>
  
  <p>Using a custom ValueResolver I can convert a list of ints to a list of Departments, but I'd like to get the Departments from the database, not create new ones.</p>
  
  <p>This is the solution I came up with, but I'm pretty sure that there's a better way to do it:</p>
  
  <pre><code>var reg= Mapper.Map&lt;Registration&gt;(dto);
  
  reg.Departments = new List&lt;int&gt;(dto.Departments).ConvertAll(input =&gt; Context.Departments.Find(input));
  
  if(reg.Departments.Contains(null)) //a department provided does not exist in the database
      return Request.CreateResponse(HttpStatusCode.BadRequest, "invalid department");
  
  ...
  </code></pre>
  
  <p>Anybody can help me out with this?</p>