Inconsistent behavior in Automapper

A very strange event happened in my project, I have fairly simple CLR objects. The first one is Model other ViewModel , after I compile the project, I started the WebApi ASP.NET project with the required parameters, I see that my Model returns data.

As soon as I see that Mapper did the mapping in order and the second time it returns every thing with zeros. A problem that does not always occur.

Very important: update 03/14/2013
This stops doing this when I recycle the application, but after a while it starts to do it again, I save the web.config again then ok again.

Here is my model / ViewModel:

 public class Gallery : Entity { public override long Id { get; set; } public virtual Settings SiteOwner { get; set; } public virtual Category Category { get; set; } public virtual string PageTitle { get; set; } public virtual string TitleDescription { get; set; } public virtual string GalleryTitle { get; set; } public virtual IList<UIItem> GalleryItems { get; set; } } public class UIItem : Entity { public override long Id { get; set; } public virtual Product Product { get; set; } public virtual Gallery Gallery { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual string Price { get; set; } public virtual string ImageUrl { get; set; } public virtual string VideoUrl { get; set; } public virtual string FileUrl { get; set; } } public class GalleryViewModel { public virtual string PageTitle { get; set; } public virtual string TitleDescription { get; set; } public virtual string GalleryTitle { get; set; } public virtual IList<UIItemViewModel> GalleryItems { get; set; } } public class UIItemViewModel { public virtual long Id { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual string Price { get; set; } public virtual string ImageUrl { get; set; } public virtual string VideoUrl { get; set; } public virtual string FileUrl { get; set; } } 

This is how i use it

 // my apicontroller // FindGalleryByAppIdAndCategoryId returns => Gallery var source = _galleryRepository.FindGalleryByAppIdAndCategoryId(appId, catId); return Mapper.DynamicMap<GalleryViewModel>(source); 
+4
source share
3 answers

Most likely this happens when Gallery and UIItem have cross-references:

 public class Gallery : Entity { public virtual IList<UIItem> GalleryItems { get; set; } } public class UIItem : Entity { public virtual Gallery Gallery { get; set; } } 

Can you check this case in your code?

+2
source
  • Why did you tag your navigation properties with the virtual ? Only properties related to one-to-one, one-to-many, many-to-many and complex types can be marked with the virtual to use lazy loading.

2.

 Mapper.CreateMap<UIItem,UIItemViewModel>(); ... UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem); 

If you need special display logic, you can do the following:

 Mapper.CreateMap<UIItem,UIItemViewModel>().ConvertUsing(new ModelToViewModelConverter()); ... UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem); ... 

The converter will look like this:

 public class ModelToViewModelConverter : ITypeConverter<UIItem,UIItemViewModel> { public UIItemViewModel Convert(ResolutionContext context) { UIItem item = (UIItem)context.SourceValue; // Perform your convertion logic } } 

In the same way, you can map all your objects, even a collection, into a collection.

+3
source

Can you try using

 Mapper.Map<Source,Destination>(source); 

or

 Mapper.Map(source); 

and make sure you announce

 CreateMap<Source, Destination>(); 

I see no reason to use Dynamic Map when you can declare CreateMap of two types. You should not experience this behavior in this way.

+1
source

All Articles