title: How to configure AutoMapper to set a property to a new instance of an object?
body: <p>I am trying to use <a href="https://automapper.org/" rel="nofollow noreferrer">AutoMapper</a> to auto-map my model to view models.</p>
  
  <p>I have the following two view-models</p>
  
  <pre><code>public class CreateComment
  {
      [Required]
      public int BlogId { get; set; }
  
      [Required]
      public string Message { get; set; }
  
      public CreateComment()
      {
  
      }
  
      public CreateComment(int? blogId)
      {
          if(blogId.HasValue)
          {
              BlogId = blogId.Value;
          }
      }
  }
  
  public class BlogViewModel 
  {
      [Required]
      public string Title { get; set; }
  
      [Required]
      public string Body { get; set; }
      // There are more
  
      [Required]
      public int? Id { get; set; }
  
      [DataType("CommentDisplayViewModelTable")]
      public IEnumerable&lt;CommentDisplayViewModel&gt; Comments { get; set; }
  
      public CreateComment CreateCommentForm { get; set; }
  }
  </code></pre>
  
  <p>The <code>CreateCommentForm</code> property should be a new instance every time this object is mapped. The only different when creating a new instance of <code>CreateCommentForm</code> I want the <code>BlogId</code> to get populated for me. The idea here is to have an empty <code>CreateCommentForm</code> to construct the html-form.</p>
  
  <p>For now, when I configure the mapper, I ignore the <code>CreateCommentForm</code> property just to get away from running into AutoMapper exceptions. But how can I tell the auto-mapper to make a new instance every time by passing the Id property to the object?</p>
  
  <pre><code>mapper.CreateMap&lt;Blog, BlogViewModel&gt;().ForMember(dest =&gt; dest.CreateCommentForm , opts =&gt; opts.Ignore() );
  </code></pre>
  
  <p>How can I correctly map the <code>CreateCommentForm</code> to a new instance by passing down the <code>Blog.Id</code> property using AutoMapper?</p>