忽略自动映射器中内部列表属性的映射 --EF6,C#



我有一个像这样的 DTO 集合属性

 public ICollection<Applicant> Applicants{get;set;}

申请人模型

public class Applicant
{
   public int Id{get;set;}
   public string name{get;set;}
   public ICollection<ApplicantSkillsVM> ApplicantSkills { get; set; }
}
public class ApplicantSkillsVM
{
   public int Id {get;set;}
   public Skill skill{get;set;}
}

我想将我的List<iApplicant> DTO映射到实体,因为我想采用申请人技能VM,但忽略ApplicantSkillsVM内部的skill

我有一个列表List<Applicant>的模型,其中包含另一个列表List<ApplicantSkillsVM>并且申请人技能虚拟机具有属性skill。我想在映射时忽略这个(skill(。它很简单。

如何在最新的带有 EF6 的自动映射器版本中执行此操作?

下面是一个运行示例:

 internal class Program
    {
        #region Methods
        private static void Main(string[] args)
        {
            // Configure the mappings
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<ApplicantSkillVM, ApplicantSkill>().ForMember(x => x.Skill, x => x.Ignore()).ReverseMap();
                cfg.CreateMap<ApplicantVM, Applicant>().ReverseMap();
            });

            var config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
            var mapper = config.CreateMapper();
            ApplicantVM ap = new ApplicantVM
            {
                Name = "its me",
                ApplicantSkills = new List<ApplicantSkillVM>
                {
                    new ApplicantSkillVM {SomeInt = 10, SomeString = "test", Skill = new Skill {SomeInt = 20}},
                    new ApplicantSkillVM {SomeInt = 10, SomeString = "test"}
                }
            };
            List<ApplicantVM> applicantVms = new List<ApplicantVM> {ap};
            // Map
            List<Applicant> apcants = Mapper.Map<List<ApplicantVM>, List<Applicant>>(applicantVms);
        }
        #endregion
    }

    /// Your source classes
    public class Applicant
    {
        #region Properties
        public List<ApplicantSkill> ApplicantSkills { get; set; }
        public string Name { get; set; }
        #endregion
    }
    public class ApplicantSkill
    {
        #region Properties
        public Skill Skill { get; set; }
        public int SomeInt { get; set; }
        public string SomeString { get; set; }
        #endregion
    }
    // Your VM classes
    public class ApplicantVM
    {
        #region Properties
        public List<ApplicantSkillVM> ApplicantSkills { get; set; }
        public string Description { get; set; }
        public string Name { get; set; }
        #endregion
    }

    public class ApplicantSkillVM
    {
        #region Properties
        public Skill Skill { get; set; }
        public int SomeInt { get; set; }
        public string SomeString { get; set; }
        #endregion
    }
    public class Skill
    {
        #region Properties
        public int SomeInt { get; set; }
        #endregion
    }
}

最初我的模型ApplicantSkillsVM没有技能的参考Id,应该nullable

所以我的模型必须看起来像

  public class ApplicantSkillsVM{
  public int Id {get;set;}
  public int? skillId{get;set;} //updated property
  public Skill skill{get;set;}
 }

问题已解决

最新更新