DISCLAIMER: This is to copy a copy from the old stackoverflow entry, which is no longer available, but I have the same problem, so it would be advisable to recompile it since it never responded.
I have a stored procedure that will return 4 sets of results (contacts, addresses, email, phones) that are populated into the data set. I would like to use AutoMapper to populate a complex object.
public class Contact { public Guid ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Address> Addresses { get; set; } public List<Phone> Phones { get; set; } public List<Email> 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; } }
I have a method that will receive data and return a list of contacts. After populating the DataSet, I define the relationships between the tables.
I found many examples where you convert a DataSet (or table) to a reader using the CreateDataReader method, and this is what I am doing here. The method will actually analyze the first table in the object, but will not list through related tables.
public List<Contact> GetContacts() { List<Contact> theList = null;
It seems to me that I'm missing something in the mapping for the Contact object, but I just can't find a good example.
If I manually populate the contact object and then pass it to my controller, it will load the ContactModel object correctly using direct matching
public ActionResult Index() {
I want to make it even possible?
source share