My database:
public class Product
{
public int Id { get; set;}
public ICollection<FieldValue> FieldValues { get; set;}
// other properties
}
public class FieldValue
{
public int Id { get; set; }
public int FieldId { get; set; }
public int ProductId { get; set; }
public Field Field { get; set; }
public Product Product { get; set;}
}
public class Field
{
public int Id { get; set;}
public string Name { get; set;}
}
My view model and mapping:
public class ProductVm
{
public IEnumerable<FieldValue> FieldValues { get; set; }
// other view models
}
this.CreateMap<Product, ProductVm>()
// other properties mappings
;
As you can see, I use FieldValue entity class in my ProductVm view model.
My database query is very simple:
var productViewModel = context.Products.ProjectTo<ProductVm>().First(p => p.Id == productId);
When I query database and project Product -> ProductVm I find FieldValue collection without Field set.
I have to use ProjectTo mapping, because there are other properties that have to be mapped to view models. Only FieldValue has to stay at it is.
How to force AutoMapper to include this Field in returned data? One restriction - I have to return FieldValue class objects. I cannot change returned class to some other VM.