我遇到一个问题,即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
答案 0 :(得分:1)
将ignore添加到子类的映射时是否有帮助?
Mapper.CreateMap<BasicTaskViewModel, BasicTask>()
.ForMember(dest => dest.Project, opt => opt.Ignore());