अस्वीकरण के साथ डेटासेट को मैप करने के लिए ऑटोमैपर का उपयोग कैसे करूं: यह एक पुराने स्टैक ओवरफ्लो पोस्ट से एक कॉपी पेस्ट है जो अब उपलब्ध नहीं है, लेकिन मेरे पास एक ही समस्या है, इसलिए इसे दोबारा पोस्ट करना उचित लगता है क्योंकि इसका कभी जवाब नहीं दिया गया था।मैं एकाधिक टेबल
मेरे पास एक संग्रहीत प्रक्रिया है जो 4 परिणाम सेट (संपर्क, पते, ईमेल, फोन) लौटाएगी जो डेटासेट में आबादी में आती है। मैं एक जटिल वस्तु को पॉप्युलेट करने के लिए ऑटोमैपर का उपयोग करना चाहता हूं।
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; }
}
मेरे पास एक तरीका है जो डेटा प्राप्त करेगा और संपर्क की एक सूची वापस कर देगा। डेटासेट पॉप्युलेट होने के बाद, मैं तालिकाओं के बीच संबंधों को परिभाषित करता हूं।
मुझे कई उदाहरण मिले हैं जहां आप DataSet (या तालिका) को CreateDataReader विधि का उपयोग करके एक पाठक में कनवर्ट करते हैं और यही वह है जो मैं यहां कर रहा हूं। विधि वास्तव में वस्तु में पहली तालिका को पार्स करेगा, लेकिन संबंधित तालिकाओं के माध्यम से गणना नहीं करेगा।
public List<Contact> GetContacts()
{
List<Contact> 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<IDataReader, Contact>(); // I think I'm missing something here
Mapper.CreateMap<IDataReader, Address>();
Mapper.CreateMap<IDataReader, Email>();
Mapper.CreateMap<IDataReader, Phone>();
//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<List<Contact>>(dr);
}
return (theList);
}
मुझे लगता है कि मुझे संपर्क ऑब्जेक्ट के लिए मैपिंग में कुछ याद आ रहा है, लेकिन मुझे बस पालन करने के लिए एक अच्छा उदाहरण नहीं मिल रहा है।
मैं मैन्युअल रूप से संपर्क वस्तु को पॉप्युलेट और फिर पारित मेरी नियंत्रक करने के लिए है, तो यह ठीक से ContactModel वस्तु एक सीधा मानचित्रण
public ActionResult Index()
{
//From the ContactController
Mapper.CreateMap<Contact, Models.ContactModel>();
Mapper.CreateMap<Address, Models.AddressModel>();
List<Models.ContactModel> theList = Mapper.Map<List<Contact>, List<Models.ContactModel>>(contacts);
return View(theList);
}
का उपयोग कर क्या मैं भी संभव करना चाहता हूँ लोड होगा?
मुझे डर था कि यह जवाब होगा, उदाहरण के लिए धन्यवाद! –