c# - AutoMapper overrides source properties -


i have following code update student entity:

using static automapper.mapper; ... public void update(studentinputdto dto) {    student student = _studentrepository.getstudent();    student = map<student>(dto); //student.studentnumer null here :(    _studentrepository.update(student); } 

my student entity:

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

my studentinputdto:

public class studentinputdto {    public string firstname { get; set; }    public string lastname { get; set; }    public int age { get; set; }    public guid genderid { get; set; } } 

the problem student.studentnumber null after mapping.

i want configure automapper in such way student.studentnumber preserved after mapping. how ? appreciated.

my initial thought configure automapper in following way:

mapper.initialize(cfg => {    cfg.createmap<studentinputdto, student>()       .formember(dest => dest.studentnumber, opt => opt.ignore()); }); 

but configuration, not solve problem.

take @ method description of automapper.

tdestination map<tdestination>(object source); 

execute mapping source object new destination object. source type inferred source object.

student = map<student>(dto) create new student object , assign student variable

to map 2 existing objects use mapper.map(dto, student); instead

tdestination map<tsource, tdestination>(tsource source, tdestination destination) 

execute mapping source object existing destination object.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -