title: How do I use automapper to map a dataset with multiple tables
body: <p><strong>DISCLAIMER: this is a copy paste from an older stackoverflow post that isn't available anymore, but I have exaclty the same problem, so it seemed appropriate to repost it as it was never answered.</strong></p>
  
  <p>I have a stored procedure that will return 4 result sets (contacts, addresses, email, phones) which is populated into a dataset. I would like to use AutoMapper to populate a complex object.</p>
  
  <pre><code>public class Contact 
  {
      public Guid ContactId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public List&lt;Address&gt; Addresses { get; set; }
      public List&lt;Phone&gt; Phones { get; set; }
      public List&lt;Email&gt; Emails { get; set; }
  }
  
  public partial class Address:BaseClass
  {
      public Guid ContactId { get; set; }
      public string Address1 { get; set; }
      public string Address2 { get; set; }
      public string Address3 { get; set; }
      public string City { get; set; }
      public string StateProvince { get; set; }
      public string PostalCode { get; set; }
      public string CountryCode { get; set; }   
  }
  
  public class Email
  {
      public Guid EmailId { get; set; } 
      public Guid ContactId { get; set; } 
      public string EmailAddress { get; set; }
  }
  
  public class Phone
  {
      public Guid PhoneId { get; set; } 
      public Guid ContactId { get; set; }         
      public string Number { get; set; } 
      public string Extension { get; set; }
  }
  </code></pre>
  
  <p>I have a method that will get data and return a list of contact. After the DataSet is populate, I define the relationships between the tables.</p>
  
  <p>I found many examples where you convert the DataSet (or table) to a reader using the CreateDataReader method and that is what I'm doing here. The method will in fact parse the first table into the object, but will not enumerate through the related tables.</p>
  
  <pre><code>public List&lt;Contact&gt; GetContacts()
  {
      List&lt;Contact&gt; theList = null;
  
      // Get the data
      Database _db = DatabaseFactory.CreateDatabase();
      DataSet ds = db.ExecuteDataSet(CommandType.StoredProcedure, "GetContacts");
  
      //The dataset should contain 4 tables
      if (ds.Tables.Count == 4) 
      {    
          //Create the maps
          Mapper.CreateMap&lt;IDataReader, Contact&gt;(); // I think I'm missing something here
          Mapper.CreateMap&lt;IDataReader, Address&gt;();
          Mapper.CreateMap&lt;IDataReader, Email&gt;();
          Mapper.CreateMap&lt;IDataReader, Phone&gt;();
  
          //Define the relationships        
          ds.Relations.Add("ContactAddresses", ds.Tables[0].Columns["ContactId"], ds.Tables[1].Columns["ContactId"]);
          ds.Relations.Add("ContactEmails", ds.Tables[0].Columns["ContactId"], ds.Tables[2].Columns["ContactId"]);
          ds.Relations.Add("ContactPhones", ds.Tables[0].Columns["ContactId"], ds.Tables[3].Columns["ContactId"]);
  
          IDataReader dr = ds.CreateDataReader();
          theList = Mapper.Map&lt;List&lt;Contact&gt;&gt;(dr);    
      }
  
      return (theList);    
  }
  </code></pre>
  
  <p>I feel as though I'm missing something in the mapping for the Contact object, but I just can't find a good example to follow.</p>
  
  <p>If I manually populate the contact object and then pass is to my controller, it will properly load the ContactModel object using a direct mapping</p>
  
  <pre><code>public ActionResult Index()
  {
      //From the ContactController
      Mapper.CreateMap&lt;Contact, Models.ContactModel&gt;();
      Mapper.CreateMap&lt;Address, Models.AddressModel&gt;();
  
      List&lt;Models.ContactModel&gt; theList = Mapper.Map&lt;List&lt;Contact&gt;, List&lt;Models.ContactModel&gt;&gt;(contacts);
  
      return View(theList);
  }
  </code></pre>
  
  <p>Is what I want to do even possible?</p>