title: Automapper with Entity Framework, Initialization Error
body: <p>I have an EF6 MVC app using Code First to generate the models.  I am trying to create a mapping using AutoMapper between the model and a view model representation of that model.  However, when I do the mapping I receive the error:</p>
  
  <blockquote>
    <p>Mapper not initialized. Call Initialize with appropriate
    configuration. If you are trying to use mapper instances through a
    container or otherwise, make sure you do not have any calls to the
    static Mapper.Map methods, and if you're using ProjectTo or
    UseAsDataSource extension methods, make sure you pass in the
    appropriate IConfigurationProvider instance.</p>
  </blockquote>
  
  <p>I am definitely initializing the Mapper, but I am not able to figure out why the configuration is failing.  Could anyone provide some assistance on what I can do to successfully use AutoMapper in this situation?</p>
  
  <p>Here is the code for how I am configuring and initializing the Mapper:</p>
  
  <pre><code>    var pTConnections = _db.PTConnections.Include(p =&gt; p.PTConnectionClass).ToList();
  
          Mapper.Initialize(cfg =&gt;
          {
              cfg.CreateMap&lt;PTConnection, PTConnectionViewModel&gt;()
                  .AfterMap((s, d) =&gt; { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });
  
              cfg.CreateMap&lt;PTCredentialAssignment, PTCredentialAssignmentViewModel&gt;()
                  .ForMember(m =&gt; m.PTCredential, opt =&gt; opt.Ignore())
                  .ForMember(m =&gt; m.PTConnection, opt =&gt; opt.Ignore());
  
              cfg.CreateMap&lt;PTVendor, PTVendorViewModel&gt;()
                  .ForMember(m =&gt; m.PTCredentials, opt =&gt; opt.Ignore())
                  .ForMember(m =&gt; m.PTConnections, opt =&gt; opt.Ignore());
  
              cfg.CreateMap&lt;PTCredential, PTCredentialViewModel&gt;();
  
              cfg.CreateMap&lt;PTConnectionClass, PTConnectionClassViewModel&gt;()
                  .ForMember(m =&gt; m.PTConnections, opt =&gt; opt.Ignore());
  
              cfg.CreateMap&lt;PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel&gt;()
                  .ForMember(m =&gt; m.PTConnection, opt =&gt; opt.Ignore());
  
          });
  
      var dest = Mapper.Map&lt;List&lt;PTConnection&gt;, List&lt;PTConnectionViewModel&gt;&gt;(pTConnections);
  </code></pre>
  
  <p>Here is my Code First Model:</p>
  
  <pre><code>public partial class PTConnection
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public PTConnection()
      {
          PTConnectionAssignments = new HashSet&lt;PTConnectionAssignment&gt;();
          PTCredentialAssignments = new HashSet&lt;PTCredentialAssignment&gt;();
          PTConnectionContactAssignments = new HashSet&lt;PTConnectionContactAssignment&gt;();
      }
  
      [Key]
      public int PTCID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(255)]
      public string ConnectionName { get; set; }
  
      [Required]
      [StringLength(100)]
      public string Host { get; set; }
  
      public int Port { get; set; }
  
      public string Comment { get; set; }
  
      public int PTCCID { get; set; }
  
      public bool? IsDisabled { get; set; }
  
      public int PTVID { get; set; }
  
      public virtual PTConnectionClass PTConnectionClass { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTConnectionAssignment&gt; PTConnectionAssignments { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTCredentialAssignment&gt; PTCredentialAssignments { get; set; }
  
      public virtual PTVendor PTVendor { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTConnectionContactAssignment&gt; PTConnectionContactAssignments { get; set; }
  }
  
  public partial class PTCredential
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public PTCredential()
      {
          PTCredentialAssignments = new HashSet&lt;PTCredentialAssignment&gt;();
          PTCredentialContactAssignments = new HashSet&lt;PTCredentialContactAssignment&gt;();
      }
  
      [Key]
      public int PTCRID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(100)]
      public string CredUsername { get; set; }
  
      [Required]
      [StringLength(500)]
      public string CredPassword { get; set; }
  
      public string Directory { get; set; }
  
      public bool? IsDisabled { get; set; }
  
      public string Comments { get; set; }
  
      public int? PTVID { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTCredentialAssignment&gt; PTCredentialAssignments { get; set; }
  
      public virtual PTVendor PTVendor { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTCredentialContactAssignment&gt; PTCredentialContactAssignments { get; set; }
  }
  
  public partial class PTVendor
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public PTVendor()
      {
          PTConnections = new HashSet&lt;PTConnection&gt;();
          PTCredentials = new HashSet&lt;PTCredential&gt;();
          PTIneligableCredentialVendorAssignments = new HashSet&lt;PTIneligableCredentialVendorAssignment&gt;();
          PTVendorAssignments = new HashSet&lt;PTVendorAssignment&gt;();
          PTVendorContactAssignments = new HashSet&lt;PTVendorContactAssignment&gt;();
      }
  
      [Key]
      public int PTVID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(50), Display(Name="Vendor Name")]
      public string Name { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTConnection&gt; PTConnections { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTCredential&gt; PTCredentials { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTIneligableCredentialVendorAssignment&gt; PTIneligableCredentialVendorAssignments { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTVendorAssignment&gt; PTVendorAssignments { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTVendorContactAssignment&gt; PTVendorContactAssignments { get; set; }
  }
  
  [Table("PTCredentialAssignment")]
  public partial class PTCredentialAssignment
  {
      [Key]
      public int PTCRAID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      public int PTCID { get; set; }
  
      public int PTCRID { get; set; }
  
      public virtual PTConnection PTConnection { get; set; }
  
      public virtual PTCredential PTCredential { get; set; }
  }
  
  [Table("PTConnectionClass")]
  public partial class PTConnectionClass
  {
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
      public PTConnectionClass()
      {
          PTConnections = new HashSet&lt;PTConnection&gt;();
      }
  
      [Key]
      public int PTCCID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Class { get; set; }
  
      [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
      public virtual ICollection&lt;PTConnection&gt; PTConnections { get; set; }
  }
  
  [Table("PTConnectionContactAssignment")]
  public partial class PTConnectionContactAssignment
  {
      [Key]
      public int PTCCAID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      public int PTCID { get; set; }
  
      [Required]
      [StringLength(60)]
      public string FirstName { get; set; }
  
      [Required]
      [StringLength(60)]
      public string LastName { get; set; }
  
      [Required]
      [StringLength(10)]
      public string PhoneNumber { get; set; }
  
      [StringLength(20)]
      public string Extension { get; set; }
  
      [StringLength(100)]
      public string Email { get; set; }
  
      [StringLength(60)]
      public string Title { get; set; }
  
      public string Comment { get; set; }
  
      public virtual PTConnection PTConnection { get; set; }
  }
  </code></pre>
  
  <p>Here is my ViewModel:</p>
  
  <pre><code>   public partial class PTConnectionViewModel
      {
          [Key]
          public int PTCID { get; set; }
  
          [Column(TypeName = "datetime2")]
          public DateTime ModifiedDate { get; set; }
  
          [Required]
          [StringLength(500)]
          public string ModifiedBy { get; set; }
  
          [Required]
          [StringLength(255)]
          public string ConnectionName { get; set; }
  
          [Required]
          [StringLength(100)]
          public string Host { get; set; }
  
          public int Port { get; set; }
  
          public string Comment { get; set; }
  
          public int PTCCID { get; set; }
  
          public bool? IsDisabled { get; set; }
  
          public int PTVID { get; set; }
  
          public virtual PTConnectionClassViewModel PTConnectionClass { get; set; }
  
          public virtual ICollection&lt;PTConnectionAssignmentViewModel&gt; PTConnectionAssignments { get; set; }
  
          public virtual ICollection&lt;PTCredentialAssignmentViewModel&gt; PTCredentialAssignments { get; set; }
  
          public virtual PTVendorViewModel PTVendor { get; set; }
  
          public virtual ICollection&lt;PTConnectionContactAssignmentViewModel&gt; PTConnectionContactAssignments { get; set; }
      }
  
  public partial class PTVendorViewModel
  {
      [Key]
      public int PTVID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(50), Display(Name="Vendor Name")]
      public string Name { get; set; }
  
      public virtual ICollection&lt;PTConnectionViewModel&gt; PTConnections { get; set; }
  
      public virtual ICollection&lt;PTCredentialViewModel&gt; PTCredentials { get; set; }
  
  }
  
  public partial class PTCredentialViewModel
  {
      [Key]
      public int PTCRID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(100)]
      public string CredUsername { get; set; }
  
      [Required]
      [StringLength(500)]
      public string CredPassword { get; set; }
  
      public string Directory { get; set; }
  
      public bool? IsDisabled { get; set; }
  
      public string Comments { get; set; }
  
      public int? PTVID { get; set; }
  
      public virtual ICollection&lt;PTCredentialAssignmentViewModel&gt; PTCredentialAssignments { get; set; }
  
      public virtual PTVendorViewModel PTVendor { get; set; }
  
      public virtual ICollection&lt;PTCredentialContactAssignmentViewModel&gt; PTCredentialContactAssignments { get; set; }
  }
  
  public partial class PTCredentialAssignmentViewModel
  {
      [Key]
      public int PTCRAID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      public int PTCID { get; set; }
  
      public int PTCRID { get; set; }
  
      public virtual PTConnectionViewModel PTConnection { get; set; }
  
      public virtual PTCredentialViewModel PTCredential { get; set; }
  }
  
  public partial class PTConnectionClassViewModel
  {
  
      [Key]
      public int PTCCID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Class { get; set; }
  
      public virtual ICollection&lt;PTConnectionViewModel&gt; PTConnections { get; set; }
  }
  
  public partial class PTConnectionContactAssignmentViewModel
  {
      [Key]
      public int PTCCAID { get; set; }
  
      [Column(TypeName = "datetime2")]
      public DateTime ModifiedDate { get; set; }
  
      [Required]
      [StringLength(500)]
      public string ModifiedBy { get; set; }
  
      public int PTCID { get; set; }
  
      [Required]
      [StringLength(60)]
      public string FirstName { get; set; }
  
      [Required]
      [StringLength(60)]
      public string LastName { get; set; }
  
      [Required]
      [StringLength(10)]
      public string PhoneNumber { get; set; }
  
      [StringLength(20)]
      public string Extension { get; set; }
  
      [StringLength(100)]
      public string Email { get; set; }
  
      [StringLength(60)]
      public string Title { get; set; }
  
      public string Comment { get; set; }
  
      public virtual PTConnectionViewModel PTConnection { get; set; }
  }
  </code></pre>