- Oct 30, 2000
- 14,665
- 440
- 126
So here is my issue in a small break down way. Collections within an object aren't mapping.
I am mapping those to the following made model classes
I am trying to setup my App_Start class to map those out properly
Of course when I instantiate and try to call the map I get a stack overflow since the collection of DBObjectOne, called ListOfObjects, doesn't map to the model class, ListOfObjectModels. When I googled the fix, I tried several of the "answers" I found but nothing seemed to work. Either I'd get an argument except or stackoverflow exception. Basically these are the answers I tried.
I've also tried
I tried various ways to get those lists to map internally to each other, but am having no luck with the various "solutions" I've found. The problem is most of the solutions are for versions previous to 4.2 where the Mapper class was an already instantiated static class and worked a bit differently than the current version where you instantiated a version of the IMapper class now. I just make that static and use it through injection in other classes. Usually I would work my objects to not need mapping back and forth and thus do what I needed through reflection instead in a single loops. But since I'm doing several maps back and forth throughout this application for various reasons I am using the AutoMapper package as it is a bit better in overhead and performance in that regard that what I could whip up in time for this current project. Still, this annoyance is stumping me and the official documentation doesn't seem to be much of a help either at this moment.
Any ideas here?
Code:
public partial class DBObjectOne
{
public int intValue { get; set; };
public string stringValue { get; set; };
public DBObjectTwo reference { get; set; };
}
public partial class DBObjectTwo
{
public string nameValue { get; set; };
public virtual ICollection<DBObjectOne> ListOfObjects { get; set; };
}
I am mapping those to the following made model classes
Code:
public class DBObjectOneModel
{
public int intValue { get; set; };
public string stringValue { get; set; };
public DBObjectTwoModel reference { get; set; };
}
public class DBObjectTwoModel
{
public string nameValue { get; set; };
public ICollection<DBObjectOneModel> ListOfObjectModels { get; set; };
}
I am trying to setup my App_Start class to map those out properly
Code:
public static class AutoMapperConfig
{
public static MapperConfiguration MapperConfig;
public static void RegisterMappings()
{
MapperConfig = MapperConfiguration( cfg => {
cfg.CreateMap<DBObjectOne, DBObjectOneModel>();
cfg.CreateMap<DBObjectOneModel, DBObjectOne>();
cfg.CreateMap<DBObjectTwo, DBObjectTwoModel>();
cfg.CreateMap<DBObjectTwoModel, DBObjectTwo>();
});
}
}
Of course when I instantiate and try to call the map I get a stack overflow since the collection of DBObjectOne, called ListOfObjects, doesn't map to the model class, ListOfObjectModels. When I googled the fix, I tried several of the "answers" I found but nothing seemed to work. Either I'd get an argument except or stackoverflow exception. Basically these are the answers I tried.
Code:
cfg.CreateMap<DbObjectTwo, DBObjectTwoModel().ForMember(dest => dest.ListOfObjectModels, opt => opt.MapFrom(src => src.ListOfObjects));
I've also tried
Code:
cfg.CreateMap<DbObjectTwo, DBObjectTwoModel().ForMember(dest => dest.ListOfObjectModels, opt => opt.MapFrom(src => Mapper.Map<IEnumerable<ListOfObjectModels>>(src.ListOfObjects)));
I tried various ways to get those lists to map internally to each other, but am having no luck with the various "solutions" I've found. The problem is most of the solutions are for versions previous to 4.2 where the Mapper class was an already instantiated static class and worked a bit differently than the current version where you instantiated a version of the IMapper class now. I just make that static and use it through injection in other classes. Usually I would work my objects to not need mapping back and forth and thus do what I needed through reflection instead in a single loops. But since I'm doing several maps back and forth throughout this application for various reasons I am using the AutoMapper package as it is a bit better in overhead and performance in that regard that what I could whip up in time for this current project. Still, this annoyance is stumping me and the official documentation doesn't seem to be much of a help either at this moment.
Any ideas here?
Last edited: