I have the below viewmodel which is same as domain model (but includes some extra properties).
public class ProductViewModel
{
public string Name { get; set; }
public string ShortDescription { get; set; }
public string FullDescription { get; set; }
public string AdminComment { get; set; }
}
From above, I am only using some of the properties in my view page.
While I am using Automapper to map my existing model, all my viewmodel fields to getting mapped to the domainmodel fields.
Mapper.Map(productViewModel, product);
Due to the above mapping all the unused viewmodel fields (by default unused viewmodel fields have NULL values) are mapped to the domain model. It is replacing my existing database data with NULL values.
Is there any way to exclude NULL and default property values from mapping?
Notes:
- I came across Using Automapper to update an existing Entity POCO
- If we use hidden fields for the excluded properties the above issue will be fixed because the viewmodel will carry the data using the hidden fields. But I am not preferring it!
I tried the below code which is not working:
Mapper.CreateMap<ProductViewModel, Product>() .ForAllMembers(opt => opt.Condition(srs => (!srs.IsSourceValueNull || IsDefaultValue(srs.SourceValue, srs.SourceType))));
Edit:
After I tried Automapper skip null values with custom resolver (Thanks abatishchev). I have used the resolver but getting error Missing type map configuration or unsupported mapping
My code snippet:
...
public System.DateTime CreatedOnUtc { get; set; }
public System.DateTime UpdatedOnUtc { get; set; }
public virtual ICollection<BackInStockSubscription> BackInStockSubscriptions { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
While the resolver accessing the BackInStockSubscriptions in the above code, I am getting the error.
Any clues?
`public System.DateTime CreatedOnUtc { get; set; } public System.DateTime UpdatedOnUtc { get; set; } public virtual ICollection