0

我遇到一个问题,automapper 正在尝试映射我不希望它映射的属性。我可以通过将属性重命名为其他名称来解决此问题,但我想使用该名称并且我想知道如何解决此类问题。

我的代码看起来像这样。

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"; }
        }
    }

我的任务映射看起来像(我删除了项目映射,因为我可以在没有这些映射的情况下复制问题)

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>();

我正在使用扩展方法进行映射

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

使用失败的一个例子是

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

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

对不起,很长的帖子,我只是不知道问题出在哪里。

如果我将 taskviewmodel 中的 Project 重命名为其他名称,它可以工作并且不会将 TaskViewModel.ProjectDetails 映射到我想要的 Project。

我已经尝试ForSourceMember(src => src.Project, opt => opt.Ignore())在每个可以添加的地方添加

当我运行这个时,我得到以下信息

 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
4

1 回答 1

1

将忽略也添加到子类的映射中是否有帮助?

Mapper.CreateMap<BasicTaskViewModel, BasicTask>()
      .ForMember(dest => dest.Project, opt => opt.Ignore());
于 2012-12-12T15:41:01.490 回答