'How do I map two objects when one object has no id
problem I have a dto and a main class with id and I made another dto without id. I don't want the id to be displayed on swagger
Service Layer
public async Task<SuperHeroDto> Create(SuperHeroDto dto) // class with id
{
var model = _mapper.Map<SuperHero>(dto); //Main class with id
_demoAPIDbContext.SuperHeroes.Add(model);
var saveChange = await _demoAPIDbContext.SaveChangesAsync();
return _mapper.Map(model, dto);
}
Question, how can I map the class that has no id?
The attributes of the 3 classes are all the same, only one has no id
public class CreateSuperHeroDTO
{
public string Name { get; set; }
public string FirtstName { get; set; }
public string LastName { get; set; }
public string Place { get; set; }
}
Solution 1:[1]
If you don't want the Id to be displayed just add [JsonIgnore] annotation one line before the property Id.
[JsonIgnore]
public Guid Id { get; set; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kvble |
