How Can I Create a DTO from these classes :
public class RegisterRequest : Entity
{
public int? UnitId { get; set; }
.
.
.
.
public virtual Unit Unit { get; set; }
}
and
public class Unit : Entity
{
public Unit()
{
RegisterRequests = new HashSet<RegisterRequest>();
}
public string UnitName { get; set; }
.
.
.
public virtual ICollection<RegisterRequest> RegisterRequests { get; set; }
}
that contains whole "RegisterRequest" class and "UnitName" of "Unit" class.
thank you
4 Answer(s)
-
0
Hi,
You can create RegisterRequestDto and UnitDto and relate these similar to your entities. Doesn't it work?
-
0
Thanx for your answer but, No, it doesn't work
I Can add "UnitName" to My DTO by and fill it by a foreach loop.
But I want to use AutoMapper.
-
0
I use this code to solve this:
Mapper.CreateMap<RegisterRequest, RegisterRequestOutDto>() .ForMember(dest => dest.UnitName, opt => opt.MapFrom(src => src.Unit.UnitName))
-
0
If you define UnitUnitName to your RegisterRequestDto, AutoMapper automatically fills it by convention (see automapper docs to understand why). Surely, "UnitUnitName" is not a good naming, but this comes another naming problem. UnitName should be Name in Unit class as a better practice. In that case, you can add UnitName to your RegisterRequestDto.