C# 忽略AutoMapper--EF6,C中内部列表属性的映射
我有一个像这样的DTO集合属性C# 忽略AutoMapper--EF6,C中内部列表属性的映射,c#,.net,entity-framework,entity-framework-6,automapper,C#,.net,Entity Framework,Entity Framework 6,Automapper,我有一个像这样的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; } } publi
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;}
}
我想将我的列表数据映射到实体,因为我想学习AppliantSkillSVM,但忽略AppliantSkillSVM中的技能
我有一个模型,它是列表,包含另一个列表,AppliantSkillSVM有一个属性技能。我想在映射时忽略此技能。很简单
在使用EF6的最新AutoMapper版本中,我如何做到这一点?这里是一个运行示例:
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
}
}
最初,我的模型AppliantSkillSVM没有应该为空的技能的参考Id
所以我的模特必须看起来像
public class ApplicantSkillsVM{
public int Id {get;set;}
public int? skillId{get;set;} //updated property
public Skill skill{get;set;}
}
问题解决了很抱歉,但这一行非常混乱:我想将我的列表数据映射到实体,因为我想学习AppliantSkillSVM,但忽略了AppliantSkillSVM中的技能。你能不能更深入地谈谈你想要达到的目标?为什么地图上的大写字母M有特别的重音?什么是I申请者?你的问题没有澄清,请复习。@I或者很简单。我有一个模型,它是列表,包含另一个列表,AppliantSkillSVM有一个属性技能。我想在映射时忽略此技能。很简单。你知道automapper和EF吗?类似的东西@gcores请阅读我的场景,然后让我知道,它很简单,但没有那么简单。错误:INSERT语句与外键约束FK__申请人____技能___49C3F6B7冲突。数据库JOBSSITE_db表dbo.skills列“id”中发生冲突。声明已被修改terminated@Alex. 我只测试了映射。这是一个错误,因为db约束,与映射无关。也许您应该问一下为什么会出现这种错误。如果你执行我的代码,你会看到请求的映射行为起作用了……但它仍然不起作用,同样的错误是抛出的。向下投票对你没有帮助,或者meI多次写信告诉你这不是映射问题。用错误打开新问题!这与我无关automapper@Tester行在技能表中生成,但不应生成。你想不出来。我丢失了可为空的Id,这是个小问题。但是你没有找到它。你的问题的标题是AutoMapper-EF6,C中的忽略内部列表属性的映射,所以你的答案解决了你机器上的问题,但不是关于如何忽略映射的问题!