I am new to Automapper. With below links, I am trying to understand it in action.
I am using its Automapper v 5.2.0
Here is my stuff.
https://codepaste.net/xph2oa
class Program
{
static void Main(string[] args)
{
//PLEASE IGNORE NAMING CONVENTIONS FOR NOW.Sorry!!
//on Startup
AppMapper mapperObj = new AppMapper();
mapperObj.Mapping();
DAL obj = new DAL();
var customer = obj.AddCustomers();
}
}
class Customer
{
public int CustomerId { get; set; }
public string CustName { get; set; }
}
class CustomerTO
{
public int CustId { get; set; }
public object CustData { get; set; }
}
class AppMapper
{
public void Mapping()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerTO>();
});
IMapper mapper = config.CreateMapper();
}
}
class DAL
{
public IEnumerable<CustomerTO> AddCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer() { CustName = "Ram", CustomerId = 1 });
customers.Add(new Customer() { CustName = "Shyam", CustomerId = 2 });
customers.Add(new Customer() { CustName = "Mohan", CustomerId = 3 });
customers.Add(new Customer() { CustName = "Steve", CustomerId = 4 });
customers.Add(new Customer() { CustName = "John", CustomerId = 5 });
return customers; //throws error
}
}
Error -Cannot implicitly convert type System.Collections.Generic.List' to
' System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
How do I map List<Customer>
to List<CustomerTO>
?
Please note, in Customer
I have property of type string
with name Custname
while CustomerTO
I have the property with name CustData
of type object
.
So how do I map this different name property?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…