I'm using AutoMapper 7.0.1 in an ASP CORE 2 project. I'm trying to map a class with a join table. Mapping from the entity to my view model works. When I ReverseMap() the join table is null and does not map back to the entity.
The reason behind it is I use the viewModel to get a list of int values to populate the selected values into a multiselect list.
I've gone through the documentation of AutoMapper but I must be missing something in CreateMap configurations. I've tried using ForMember but was unsuccessful.
I'm also trying to understand the flattening and unflattening process in ReverseMap()
//Article.cs
public class Article
{
public int ArticleId { get; set; }
public string Title { get; set; }
public virtual ICollection<ArticleGroup> ArticleGroups { get; set; } = new List<ArticleGroup>();
}
//Group.cs
public class Group
{
public int GroupId { get; set; }
public string GroupName { get; set; }
public virtual ICollection<ArticleGroup> ArticleGroups { get; set; } = new List<ArticleGroup>();
}
//ArticleGroup.cs
public class ArticleGroup
{
public int ArticleId { get; set; }
public Article Article { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
}
//ArticleFormView.cs
public class ArticleFormView
{
public int ArticleId { get; set; }
public string Title { get; set; }
public virtual List<int> GroupIds { get; set; }
}
//Startup.cs
...
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Article, ArticleFormView>()
.ReverseMap()
;
//Tried this as well removing ReverseMap() from above
//cfg.CreateMap<ArticleFormView, Article>()
// .ForMember(a => a.ArticleGroups, opt =>
// opt.MapFrom(src =>
// src.GroupIds
// )
// )
//;
});
...
//View Component
@model ArticleFormView
<div class="form-group">
<label asp-for="Title" class="col-xs-1 control-label"></label>
<div class="col-xs-11">
<span asp-validation-for="Title" class="text-danger"></span>
<input asp-for="Title" class="form-control">
</div>
</div>
<div class="form-group">
<label asp-for="GroupIds" class="col-xs-1 control-label"></label>
<div class="col-xs-2">
<select multiple="multiple" asp-for="GroupIds" asp-items="ViewBag.ListOfGroups" name="GroupIds[]" style="display:none"></select>
</div>
</div>
How do I configure AutoMapper to from List of int in ViewModel back into List of ArticleGroup in Article entity?