Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射覆盖源属性_C#_.net_Mapping_Automapper_Dto - Fatal编程技术网

C# 自动映射覆盖源属性

C# 自动映射覆盖源属性,c#,.net,mapping,automapper,dto,C#,.net,Mapping,Automapper,Dto,我有以下代码要更新Student实体: using static AutoMapper.Mapper; ... public void Update(StudentInputDto dto) { Student student = _studentRepository.GetStudent(); student = Map<Student>(dto); //student.studentnumer is null here :( _studentRepository

我有以下代码要更新
Student
实体:

using static AutoMapper.Mapper;
...
public void Update(StudentInputDto dto)
{
   Student student = _studentRepository.GetStudent();
   student = Map<Student>(dto); //student.studentnumer is null here :(
   _studentRepository.Update(student);
}
我的学生输入到:

public class Student
{
   public Guid studentid { get; set; }
   public string firstname { get; set; }
   public string lastname { get; set; }
   public int age { get; set; }
   public Guid genderid { get; set; }
   public string studentnumber { get; set; }
}
public class StudentInputDto
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Age { get; set; }
   public Guid GenderId { get; set; }
}
问题在于映射后Student.studentnumber为null

我希望以这样一种方式配置AutoMapper,即在映射后保留Student.studentnumber。怎么做?任何帮助都将不胜感激

我最初的想法是通过以下方式配置AutoMapper:

Mapper.Initialize(cfg => {
   cfg.CreateMap<StudentInputDto, Student>()
      .ForMember(dest => dest.studentnumber, opt => opt.Ignore());
});
Mapper.Initialize(cfg=>{
cfg.CreateMap()
.ForMember(dest=>dest.studentnumber,opt=>opt.Ignore());
});

但是这种配置并不能解决问题。

请查看Automapper的方法说明

TDestination Map<TDestination>(object source);
执行从源对象到现有目标的映射 反对


查看自动映射的方法说明

TDestination Map<TDestination>(object source);
执行从源对象到现有目标的映射 反对

可能的重复可能的重复