title: Map property to property of a collection automapper
body: <p>I have two entities </p>
  
  <pre><code>public class A{
      public string Example { get; set; }
      public ICollection&lt;B&gt; BCollection { get;set; } = new HashSet&lt;B&gt;();
  }
  
  public class B {
      public string MyProperty { get; set; }
  }
  </code></pre>
  
  <p>And a simple ViewModel </p>
  
  <pre><code>public class AFirstLoadViewModel {
      public string Example { get; set; }
      public string MyProperty { get; set; }
  }
  </code></pre>
  
  <p>The thing, is, this viewmodel will be use only in the first data entry, when <code>A</code> will only have one <code>B</code> object inside.</p>
  
  <p>So, i'm trying to map a object like this: </p>
  
  <pre><code>var source = new AFirstLoadViewModel
  {
      Example = "example",
      MyProperty = "myproperty"
  }
  </code></pre>
  
  <p>to this </p>
  
  <pre><code>var destination = new A {
      Example = "example"
      BCollection = new List&lt;B&gt; {
          new B { MyProperty = "myproperty" }
      }
  }
  </code></pre>
  
  <p>I try to do the trick using <code>ForPath</code> and <code>BeforeMap</code> without luck</p>
  
  <pre><code>CreateMap&lt;AFirstLoadViewModel, A&gt;()
      .ForMember(x =&gt; x.Example, c =&gt; c.MapFrom(x =&gt; x.Example))
      .ForPath(x =&gt; x.BCollection.First().MyProperty, c =&gt; c.MapFrom(x =&gt; x.MyProperty))
      .BeforeMap((viewModel, entity) =&gt; {
          if(!entity.BCollection.Any())
              BCollection.Add(new B());
      });
  </code></pre>
  
  <p>But i get </p>
  
  <blockquote>
    <p>System.ArgumentOutOfRangeException: Only member accesses are allowed.</p>
  </blockquote>
  
  <p>How can i deal with it?</p>
  
  <p><em>I clarify: both, view model and model have many more properties, the question classes are by way of example</em></p>
  
  <p>Edit: </p>
  
  <hr>
  
  <p>I try the solution proposed by Johnatan, and it works, the problem here, is that i cant Unit Testing anymore.</p>
  
  <p>I'm testing with </p>
  
  <pre><code>var config = new MapperConfiguration(cfg =&gt; cfg.CreateMap&lt;AFirstLoadViewModel, A&gt;(MemberList.Source));
  </code></pre>
  
  <p>And when i call <code>config.AssertConfigurationIsValid()</code> fails because the <code>MyProperty</code> property is not mapped</p>