Using the automapper should be theoretically very simple: you have two objects with same properties, you map the two and you are done.
But no, the automapper is eating my application alive and my patience too.
I already opend a question one week ago about a problem I had: Automapper fails to map two entities
One week ago it was the exclusive problem I had. I solved it by using Mapper.DynamicMap instead of Mapper.Map instead of Map, which is a pain in the ass... but I don't want to lose hours and hours of work for a stupid mapping.
In the last week the problem has proliferated like a cancer, I'm having the same problem everywhere... even in places where mapping used to work fine, and I solved all problems by using DynamicMap instead of Map.
An other example of error...
Mapping types: Product -> ProductSupplierBDO EnterpriseDataAccessLayer.EnterpriseData.Model.Products.Product -> ERP.BusinessLayer.Model.Products.ProductSupplierBDO
Destination path: ProductBDO.Tags.Tags.Tags0[0].Products.Products.Products0[0]
Source value: System.Data.Entity.DynamicProxies.Product_864C02AAA73EC52FBC9037EE9B3256F2A0E7BAE2402517C87AC80C03E65CC321
It would be a start if at least I would understand the error message, which according to me is almost incomprensehebile.
Now, this question will not be specific because the problem is general and not specific.
What I can say is that my object tree has many circular refernces (object A contains B and B contains A)... typical structure of entity framework entities.
Have you ever had similar problems with the automapper? What are the possible reasons of a failure in mappings? How does it exactly work? How does dynamic map exactly work? Does it have problems with the Guid type? Are there better mappers or tool for "deep converting"/deep cloning.
Everything it can help is appreciated.
if you want to see a part of the code simply ask.
Here below you can see the Mapper class.
public static class ProductsMapper
{
static ProductsMapper()
{
MapProductModelToBDO();
MapProductBDOToModel();
MapProductCategoryModelToBDO();
MapProductCategoryBDOToModel();
MapIvaModelToBDO();
MapIvaBDOToModel();
MapProductSupplierModelToBDO();
MapProductSupplierBDOToModel();
MapProductPictureModelToBDO();
MapProductPictureBDOToModel();
MapTagModelToBDO();
MapTagBDOToModel();
}
private static void MapTagBDOToModel()
{
Mapper.CreateMap<Product, ProductBDO>().ReverseMap();
}
private static void MapTagModelToBDO()
{
Mapper.CreateMap<Product, ProductBDO>();
}
public static TTargetType Convert<TToConvert, TTargetType>(TToConvert toConvert)
{
return Mapper.Map<TTargetType>(toConvert);
}
public static TTargetType DynamicConvert<TToConvert, TTargetType>(TToConvert toConvert)
{
return Mapper.DynamicMap<TTargetType>(toConvert);
}
private static void MapProductModelToBDO()
{
Mapper.CreateMap<Product, ProductBDO>();
}
private static void MapProductBDOToModel()
{
Mapper.CreateMap<Product, ProductBDO>().ReverseMap();
}
private static void MapProductCategoryModelToBDO()
{
Mapper.CreateMap<ProductCategory, ProductCategoryBDO>();
}
private static void MapProductCategoryBDOToModel()
{
Mapper.CreateMap<ProductCategory, ProductCategoryBDO>().ReverseMap();
}
private static void MapIvaModelToBDO()
{
Mapper.CreateMap<Iva, IvaBDO>();
}
private static void MapIvaBDOToModel()
{
Mapper.CreateMap<Iva, IvaBDO>().ReverseMap();
}
private static void MapProductSupplierModelToBDO()
{
Mapper.CreateMap<ProductSupplier, ProductSupplierBDO>();
}
private static void MapProductSupplierBDOToModel()
{
Mapper.CreateMap<ProductSupplier, ProductSupplierBDO>().ReverseMap();
}
private static void MapProductPictureModelToBDO()
{
Mapper.CreateMap<ProductPicture, ProductPictureBDO>();
}
private static void MapProductPictureBDOToModel()
{
Mapper.CreateMap<ProductPicture, ProductPictureBDO>().ReverseMap();
}
}