5
Ho diverse classi:Come mappare un oggetto complesso con Automapper?
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Product Product { get; set; }
}
public class Product
{
public string Name { get; set; }
}
public class Customer_
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Article Article { get; set; }
}
public class Article
{
public string ArticleName { get; set; }
}
e un'istanza:
Customer[] Customer = new Customer[]
{
new Customer()
{
FirstName = "FirstName1",
LastName = "LastName1",
Product = new Product()
{
Name = "Product1"
}
},
new Customer()
{
FirstName = "FirstName2",
LastName = "LastName2",
Product = new Product()
{
Name = "Product2"
}
}
};
voglio mappare questa istanza di un oggetto Customer_
dove Customer.Product.Name
= Customer_.Article.ArticleName
Io uso Automapper:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Product, Article>()
.ForMember(dest => dest.ArticleName, opt => opt.MapFrom(src => src.Name));
});
IMapper mapper = config.CreateMapper();
var desti = mapper.Map<Customer[], Customer_[]>(Customer);
Ma ho un'eccezione:
Missing type map configuration or unsupported mapping.
Mapping types:
Customer -> Customer_
Xml_Test.Customer -> Xml_Test.Customer_
Destination path:
Customer_[][0]
Source value:
Xml_Test.Customer
Cosa c'è che non va?
Basta aggiungere '' del cliente mappatura: 'cfg.CreateMap()', come ha detto nel messaggio di errore. –