title: Can automapper map a foreign key to an object using a repository?
body: <p>I'm trying out Entity Framework Code first CTP4. Suppose I have:</p>
  
  <pre><code>public class  Parent
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public class Child
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public Parent Mother { get; set; }
  }
  
  public class TestContext : DbContext
  {
      public DbSet&lt;Parent&gt; Parents { get; set; }
      public DbSet&lt;Child&gt; Children { get; set; }
  }
  
  public class ChildEdit
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int MotherId { get; set; }
  }
  
  Mapper.CreateMap&lt;Child, ChildEdit&gt;();
  </code></pre>
  
  <p>Mapping to the Edit model is not a problem. On my screen I select the mother through some control (dropdownlist, autocompleter, etc) and the Id of the mother gets posted in back:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(ChildEdit posted)
  {
      var repo = new TestContext();
  
      var mapped = Mapper.Map&lt;ChildEdit, Child&gt;(posted);  // &lt;------- ???????
  }
  </code></pre>
  
  <p>How should I solve the last mapping? I don't want to put Mother_Id in the Child object. For now I use this solution, but I hope it can be solved in Automapper.</p>
  
  <pre><code>        Mapper.CreateMap&lt;ChildEdit, Child&gt;()
              .ForMember(i =&gt; i.Mother, opt =&gt; opt.Ignore());
  
          var mapped = Mapper.Map&lt;ChildEdit, Child&gt;(posted);
          mapped.Mother = repo.Parents.Find(posted.MotherId);
  </code></pre>
  
  <p><strong>EDIT</strong>
  This works, but now I have to do that for each foreign key (BTW: context would be injected in final solution):</p>
  
  <pre><code>        Mapper.CreateMap&lt;ChildEdit, Child&gt;();
              .ForMember(i =&gt; i.Mother,
                         opt =&gt; opt.MapFrom(o =&gt; 
                                new TestContext().Parents.Find(o.MotherId)
                                           )
                        );
  </code></pre>
  
  <p>What I'd really like would be:</p>
  
  <pre><code>        Mapper.CreateMap&lt;int, Parent&gt;()
              .ForMember(i =&gt; i, 
                         opt =&gt; opt.MapFrom(o =&gt; new TestContext().Parents.Find(o))
                        );
  
          Mapper.CreateMap&lt;ChildEdit, Child&gt;();
  </code></pre>
  
  <p>Is that possible with Automapper?</p>