0

I have two entities :

public class TableA
  {

    public int id { get; set; }   
    public virtual TableB TableB { get; set; }
}



public class TableB
 {
   public TableB()
   {
     TableAList = new List<TableA>();
   }
   public int id { get; set; }
   public virtual List<TableA> TableAList { get; set; }
 }

And two classes

public class ClasseMetierA
  {
    private int id;
    public ClasseMetierB TableB;
}


public class ClasseMetierB
  {
                 public ClassMetierB()
        {
            TableAList = new List<ClasseMetierAList>();
        }
    private int id;   
    public List<ClasseMetierA> TableAList { get; set; }
}

I would like to convert TableB object into ClasseMetierB object. I'm using automapper with this configuration :

mapperConfig = new MapperConfiguration(cfg =>
        {

                    cfg.CreateMap<TableA, ClasseMetierA>().MaxDepth(3);
                    cfg.CreateMap<TableB, ClasseMetierB>().MaxDepth(3);

        });
        mapperConfig.AssertConfigurationIsValid();

I'm calling this function to convert my object :

public B ConvertDTB(D obj)
    {
      var mapper = mapperConfig.CreateMapper();
      return mapper.Map<B>(obj);
    }

The problem is the following : When I convert an TableB object into a ClasseMetierB object, the properties of all the elements in TableAList are all null, even if the sources properties (TableB.TableAList) were not.

I found this post (Nested object members null after mapping with Automapper) with a similar problem but the solution given just raise a null exception error.

Thanks in advance

3
  • A repro would help. Make a gist that we can execute and see fail. Commented Mar 20, 2019 at 9:00
  • I realized this was due to the fact my fields are private. I tried to handle that with cfg.ShouldMapProperty = pi => pi.GetMethod != null && (pi.GetMethod.IsPublic || pi.GetMethod.IsPrivate); but i'm getting a "Unmapped members were found" error Commented Mar 20, 2019 at 11:15
  • That error is telling you exactly what's unmapped. That means your config is broken and you have to fix it. Commented Mar 21, 2019 at 6:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.