title: Using Automapper - how do I map from this ICollection?
body: <p>I am using entity framework and automapper for the first time. I am lost. I have a class containing an <code>ICollection</code> of another class which I wish to map:</p>
  <p>Here are my classes:</p>
  <pre><code>public class BaseEntity {
      public int Id { get; set; }
  }
  
  public class Car : BaseEntity {
      public string Make { get; set; }
      public string Model { get; set; }
      public int Year { get; set; }
      public ICollection&lt;CarPart&gt; CarParts { get; set; }
  }
  
  public class Part : BaseEntity {
      public string PartName { get; set; }
  }
  
  public class CarPart : BaseEntity {
      public int CarId { get; set; }
      public Car Car { get; set; }
      public int PartId { get; set; }
      public Part Part { get; set; }
  }
  </code></pre>
  <p>and then my DTO:</p>
  <pre><code>public class CarDTO {
      public int Id { get; set; }
      public string Make { get; set; }
      public string Model { get; set; }
      public int Year { get; set; }
      public ICollection&lt;CarPart&gt; CarParts { get; set; }
  }
  </code></pre>
  <p>and my mapping configuration profile contains:</p>
  <pre><code>CreateMap&lt;Car, CarDTO&gt; ();
  </code></pre>
  <p>and then in my controller I use:</p>
  <pre><code>private readonly IMapper _mapper;
  return _mapper.Map&lt;Car, CarDTO&gt;(car);
  </code></pre>
  <p>When I hit the API I get the following error:</p>
  <pre><code>Destination Member:
  CarParts
   ---&gt; AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
  Mapping types:
  CarPart -&gt; Part
  Entities.CarPart -&gt; Entities.Part
  </code></pre>
  <p>What changes do I need to make to fix this?
  Thank-you.</p>
  <p>Edit:
  The following is all in StartUp.cs:</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
       services.AddAutoMapper(typeof(MappingProfiles));
  }
  </code></pre>
  <p><strong>Edit #2:</strong>
  Changing the scope of this a little bit. I changed CarDTO to</p>
  <pre><code>public class CarDTO {
      public int CarId { get; set; }
      public string Make { get; set; }
      public string Model { get; set; }
      public int Year { get; set; }
      public IReadOnlyList&lt;Part&gt; Parts { get; set; }
  }
  </code></pre>
  <p>and added the following DTO:</p>
  <pre><code> public class PartDTO
      {
          public int PartId { get; set; }
          public string PartName { get; set; }
      }
  </code></pre>
  <p>and edited my mapping configuration profile to</p>
  <pre><code>  public MappingProfiles () {
      
              CreateMap&lt;Car, CarDTO&gt; ()
  .ForMember(dest =&gt; dest.CarId, opt =&gt; opt.MapFrom(src =&gt; src.Id))
                  .ForMember (dest =&gt; dest.Parts, opt =&gt;
                      opt.MapFrom (src =&gt; src.CarParts.Select (x =&gt; x.Part
                      )));
      
              CreateMap&lt;Part, PartDTO&gt;()
              .ForMember(dest =&gt; dest.PartId, opt =&gt; opt.MapFrom(src =&gt; src.Id))
          }
  </code></pre>
  <p>which returns this:</p>
  <pre><code>{
      &quot;carId&quot;: 8,
      &quot;make&quot;: &quot;some make&quot;,
      &quot;model&quot;: &quot;some model&quot;,
      &quot;year&quot;: 2001,
      &quot;parts&quot;: [
          {
              &quot;partName&quot;: &quot;Fully sick Wheels&quot;,
              &quot;id&quot;: 3
          },
          {
              &quot;partName&quot;: &quot;Mad Engine&quot;,
              &quot;id&quot;: 4
          }
      ]
  }
  </code></pre>
  <p>How can I change this to:</p>
  <pre><code>{
      &quot;carId&quot;: 8,
      &quot;make&quot;: &quot;some make&quot;,
      &quot;model&quot;: &quot;some model&quot;,
      &quot;year&quot;: 2001,
      &quot;parts&quot;: [
          {
              &quot;partName&quot;: &quot;Fully sick Wheels&quot;,
              &quot;partId&quot;: 3
          },
          {
              &quot;partName&quot;: &quot;Mad Engine&quot;,
              &quot;partId&quot;: 4
          }
      ]
  }
  </code></pre>