0

I am using Automapper for mapping two objects.

I have field VehicleModel in the destination which has some default value. I don't have a mapping for this destination field in the source. So I didn't map it. After mapping is done, my default value is set to null value on the destination. The data objects looks like below.

public partial class Source
{

private Car[] cars;

public Car[] Cars
{
    get { return this.cars; }
    set { this.cars = value; }
}
}

public partial class Destination
{
private OutputData output;

public OutputData Output
{            
    get {  return this.output; }
    set {  this.output= value; }
}   
}

public class OutputData
{
private List<Cars> cars;
private string vehicleModel;

public Car[] Cars
{
    get { return this.cars; }
    set { this.cars = value; }
}
public string VehicleModel
{            
    get {  return this.vehicleModel; }
    set {  this.vehicleModel= value; }
}
}    

Mapping between Source and OutputData.

Mapper.CreateMap<Source, OutputData>();
Mapper.CreateMap<Source, Destination>().ForMember( dest => dest.Output, input => 
    input.MapFrom(s=>Mapper.Map<Source, OutputData>(s))); 

How to avoid this behavior.

Thanks in advance. Sandeep

k0stya
  • 4,267
  • 32
  • 41
  • 1
    It would help to see your CreateMap and your class definitions. However, are you providing your destination object when you call Mapper.Map? – PatrickSteele Jul 26 '12 at 12:33
  • Please refer to my earlier post for data model http://stackoverflow.com/questions/11633021/automapper-expression-must-resolve-to-top-level-member public class OutputData { private List cars; private string vehicleModel; public Car[] Cars { get{ return this.cars; } set { this.cars = value; } } public String VehicleModel{ get { return this.vehicleModel; } set { this.vehicleModel = value; } } } VehicleModel field has some value before mapping. It doesn't have mapping field in source. After mapping the VehicleModel is set to Null – Sandeep Reddy Pinniti Jul 26 '12 at 14:03
  • 1
    Please add the code to the question so its readable and others who may be able to help don't have to sift through comments. – PatrickSteele Jul 26 '12 at 14:30
  • I have updated the question with the code, please take a look at it. – Sandeep Reddy Pinniti Jul 27 '12 at 07:12

1 Answers1

1

I have modified the code to make it compilable. Correct if something is wrong.

public class Car
{
    public string Brand {get;set;}
}

public partial class Source
{
    private Car[] cars;

    public Car[] Cars
    {
        get { return this.cars; }
        set { this.cars = value; }
    }
}

public partial class Destination
{
    private OutputData output;

    public OutputData Output
    {            
        get {  return this.output; }
        set {  this.output= value; }
    }   
}

public class OutputData
{
    private List<Car> cars;
    private string vehicleModel = "DEFAULTMODEL";

    public Car[] Cars
    {
        get { return cars.ToArray(); }
        set { this.cars = value.ToList(); }
    }
    public string VehicleModel
    {            
        get {  return this.vehicleModel; }
        set {  this.vehicleModel= value; }
    }
}    

Note: I added default model.

With you configuration and above code the following mapping works as you expected:

var res = Mapper.Map<Source, Destination>(new Source { Cars = new Car[]{ new Car{ Brand = "BMW" }}});

So looks like you some important piece of code is not provided.

k0stya
  • 4,267
  • 32
  • 41