2

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();
    }

}
Community
  • 1
  • 1
Errore Fatale
  • 978
  • 1
  • 9
  • 21
  • did you turn off ProxyCreationEnabled in EntityFramework? – Kien Chu Oct 08 '15 at 09:20
  • I probably know what you are thinking about. It can happens that the AutomapperException is thrown cause of this: http://stackoverflow.com/questions/4867602/entity-framework-there-is-already-an-open-datareader-associated-with-this-comma I know the error well, and this is not the case. Collection of results are converted to List before to leave the data access layer, and the mappings errors occur outside of it. – Errore Fatale Oct 08 '15 at 09:31
  • The reason I asked is because I saw your error message `Source value: System.Data.Entity.DynamicProxies.Product_864C02AAA73EC52FBC9037EE9B3256F2A0E7BAE2402517C87AC80C03E65CC321`. I think you're mapping the generated product dynamic proxy object instead of product object – Kien Chu Oct 08 '15 at 09:35
  • You are right. But if I disable the dynamic proxy creation, I disable the lazy loading too. Are you suggesting to disable lazy loading? What if someone want to use lazy loading withtout conflicting with the automapper? – Errore Fatale Oct 08 '15 at 09:50
  • I think http://stackoverflow.com/questions/17825956/is-automapper-preventing-lazy-loading-with-ef can answer your problem – Kien Chu Oct 08 '15 at 10:07
  • @ErroreFatale: I might suspect your issue is a duplicate of http://stackoverflow.com/questions/11505128/circular-reference-causing-stack-overflow-with-automapper i.e. your mapper goes in circles, from product to tags and to product and ... – Wiktor Zychla Oct 08 '15 at 10:09
  • @kienct89 thanks, I finally decided to disable lazy loading. – Errore Fatale Oct 08 '15 at 10:43
  • @Wiktor Zychla the main reason I decided to use the automapper is that it is able to manage circual refernces. My entities are full of circular refernces, it's the norm when you work with entity framework. If automapper would not be able to manage circual references I would have errors everywhere, but I solved most of them by disabling lazy loading. I don't know why in this particular case the automapper wouldn't be able to manage the circular reference... if you have a rational explanation... – Errore Fatale Oct 08 '15 at 10:43

0 Answers0