Josh
Josh

Reputation: 1058

Ignoring property in automapper for a nested class

I am having an issue where automapper is trying to map a property that I do not want it to map. I can fix this issue by renaming the property to something else but I want to use the name and I want to know how to fix issues like this.

My code looks like this.

public abstract class Task
    {
        public int Id { get; set; }
        public int ProjectId { get; set; }
        public virtual Project Project { get; set; }
        public string Title { get; set; }
    }

 public class BasicTask : Task
    {
    }
public abstract class Project
    {

        public virtual int Id { get; set; }
        public virtual string Title { get; set; }
        public virtual ICollection<Task> Tasks { get; set; }
        [NotMapped]
        public abstract string ProjectTypeDescription { get; }

    }
public class BasicProject : Project
    {
        public override string ProjectTypeDescription
        {
            get { return "Basic Project"; }
        }
    }


public abstract class TaskViewModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public abstract string TaskType { get; }
        //This is the property giving me issues
        public ProjectDetails Project { get; set; }

        public class ProjectDetails
        {
            public int Id { get; set; }
            public string Title { get; set; }
        }
    }
public class BasicTaskViewModel : TaskViewModel
    {
        public override string TaskType
        {
            get { return "Basic"; }
        }
    }
public abstract class ProjectViewModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public SelectList ProjectTypes { get; set; }
        public abstract string ProjectType { get; }
        public IEnumerable<TaskViewModel> Tasks { get; set; }
    }
public class BasicProjectViewModel : ProjectViewModel
    {
        public override string ProjectType
        {
            get { return "Basic Project"; }
        }
    }

My mapping for Tasks looks like (I removed the project mapping since I can replicate the problem without those mappings)

Mapper.CreateMap<Task, TaskViewModel>()
                .ForMember(dest => dest.Project, opt => opt.Ignore())
                .Include<BasicTask, BasicTaskViewModel>();
            Mapper.CreateMap<BasicTask, BasicTaskViewModel>();

            Mapper.CreateMap<TaskViewModel, Task>()
                .Include<BasicTaskViewModel, BasicTask>();
            Mapper.CreateMap<BasicTaskViewModel, BasicTask>();

            Mapper.CreateMap<Project, TaskViewModel.ProjectDetails>();

I am use an extenstion method for the mapping

public static TResult MapTo<TResult>(this object self)
        {
            return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult));
        }

An example of using this that fails is

TaskViewModel vm = new BasicTaskViewModel()
            {
                Id = 1,
                Project = new TaskViewModel.ProjectDetails()
                {
                    Id = 1,
                    Title = "Some Title",
                }
            };

            Task m = vm.MapTo<Task>();

Sorry for the very long post I just don't know where the problem is.

If I rename Project in the taskviewmodel to something else it works and doesn't map TaskViewModel.ProjectDetails to Project which is what I want.

I have tried adding ForSourceMember(src => src.Project, opt => opt.Ignore()) in every spot that I can

When I run this I get the follwoing

 Missing type map configuration or unsupported mapping.

Mapping types:
ProjectDetails -> Project
amtest.ViewModel.TaskViewModel+ProjectDetails -> amtest.Models.Project

Destination path:
BasicTask.Project.Project

Source value:
amtest.ViewModel.TaskViewModel+ProjectDetails
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
ProjectDetails -> Project
amtest.ViewModel.TaskViewModel+ProjectDetails -> amtest.Models.Project

Destination path:
BasicTask.Project.Project

Source value:
amtest.ViewModel.TaskViewModel+ProjectDetails

Upvotes: 0

Views: 1823

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

Does it help when you add the ignore also to the mapping of the child class?

Mapper.CreateMap<BasicTaskViewModel, BasicTask>()
      .ForMember(dest => dest.Project, opt => opt.Ignore());

Upvotes: 1

Related Questions