AutoMapper not returning objects from related entites - c#

I'm having issues with returning objects from related domain models. The objects that are from other models are returning null.
What i am basically trying to accomplish is return an DTO that have the fields that i want from the related domain models instead of passing every field straight from the domain models to json.
Please see below code, can someone please advise.
## CourseDomainModels.cs ##
public class CourseDomainModel : IObjectWithState
{
public int Id { get; set; }
public string Name { get; set; }
public Double Duration { get; set; }
public string Description { get; set; }
public virtual TutorDomainModel CourseTutor { get; set; }
public virtual SubjectDomainModel CourseSubject { get; set; }
public ICollection<EnrollmentDomainModel> Enrollments { get; set; }
[NotMapped]
public Common.State state { get; set; }
[NotMapped]
public bool InDb => this.Id != default(int);
public object PersistenceEntityId => this.Id;
}
## TutorDomainModel.cs ##
public class TutorDomainModel : IObjectWithState
{
public int Id { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Enums.Gender Gender { get; set; }
public ICollection<CourseDomainModel> Courses;
[NotMapped]
public Common.State state { get; set; }
[NotMapped]
public bool InDb => this.Id != default(int);
public object PersistenceEntityId => this.Id;
}
## CourseDTO.cs ##
public class CourseDTO
{
public string Name { get; set; }
public Double Duration { get; set; }
public string Description { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
## AutoMapperConfig.cs ##
public class AutoMapperConfig
{
public static void RegisterMapping()
{
Mapper.CreateMap<CourseDomainModel, CourseDTO>();
}
}
## Startup.cs ##
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
AutoMapperConfig.RegisterMapping();
}
}
## CourseService.cs ##
public CourseDTO GetCourse(int id)
{
var course = _courseRepo.Get(id);
CourseDTO courseView = Mapper.Map<CourseDomainModel,CourseDTO(course);
return courseView;
}

AutoMapper maps the properties of TSource to properties of TDestination, but it does not try to find properties of TDestination from child properties of TSource by default.
You can instruct AutoMapper to do so:
Mapper.CreateMap<CourseDomainModel, CourseDTO>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.Email))
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.CourseTutor == null ? string.Empty : src.CourseTutor.LastName));
CourseDTO courseView = Mapper.Map<CourseDTO>(course);

AutoMapper is not AI yet, so you should explicitly specify custom member mappings:
Mapper.CreateMap<CourseDomainModel, CourseDTO>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.TutorDomainModel.Email));

Related

Map ICollection With AutoMapper

Hello Guys I'm Having a Lot of trouble mapping some models using AutoMapper and I wanted to know if you could point me in the right direction.
I have some Entities as follow;
public class Camp
{
public int CampId { get; set; }
public string Name { get; set; }
public string Moniker { get; set; }
public Location Location { get; set; }
public DateTime EventDate { get; set; } = DateTime.MinValue;
public int Length { get; set; } = 1;
public ICollection<Talk> Talks { get; set; }
}
public class Talk
{
public int TalkId { get; set; }
public Camp Camp { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public int Level { get; set; }
public Speaker Speaker { get; set; }
}
And the corresponding DTO's
public class CampModel
{
public string Name { get; set; }
public string Moniker { get; set; }
public DateTime EventDate { get; set; } = DateTime.MinValue;
public int Length { get; set; } = 1;
public string Venue { get; set; }
public string LocationAddress1 { get; set; }
public string LocationAddress2 { get; set; }
public string LocationAddress3 { get; set; }
public string LocationCityTown { get; set; }
public string LocationStateProvince { get; set; }
public string LocationPostalCode { get; set; }
public string LocationCountry { get; set; }
public ICollection<TalkModel> Talks { get; set; }
}
public class TalkModel
{
public int TalkId { get; set; }
public string Title { get; set; }
public string Abstract { get; set; }
public int Level { get; set; }
}
I wanted To use automapper on my controller as follow:
[Route("api/[controller]")]
public class CampsController : ControllerBase
{
private readonly ICampRepository _repository;
private readonly IMapper _mapper;
public CampsController(ICampRepository repository, IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
[HttpGet]
public async Task<ActionResult<CampModel[]>> Get(bool includeTalks = false)
{
try
{
var camps = await _repository.GetAllCampsAsync(includeTalks);
var mapper = _mapper.Map<CampModel[]>(camps);
return mapper;
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Database failure" + " Message: " + e);
}
}
}
I'm returning the camps on my repository like this:
public async Task<Camp[]> GetAllCampsByEventDate(DateTime dateTime, bool includeTalks = false)
{
_logger.LogInformation($"Getting all Camps");
IQueryable<Camp> query = _context.Camps
.Include(c => c.Location);
if (includeTalks)
{
query = query
.Include(c => c.Talks)
.ThenInclude(t => t.Speaker);
}
// Order It
query = query.OrderByDescending(c => c.EventDate)
.Where(c => c.EventDate.Date == dateTime.Date);
return await query.ToArrayAsync();
}
I already registered my automapper on Startup.Cs
services.AddAutoMapper(typeof(CampProfile).Assembly);
Using the profile like this:
public class CampProfile : Profile
{
public CampProfile()
{
this.CreateMap<Camp, CampModel>()
.ForMember(c => c.Venue, o => o.MapFrom(m => m.Location.VenueName))
.ForMember(c => c.Talks, o => o.MapFrom(m => m.Talks))
.ReverseMap();
}
}
But when i try to hit my endpoint i get the following error:
Message: AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
Object -> CampModel[]
System.Object -> CoreCodeCamp.Models.CampModel[]
---> AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
Camp -> CampModel
CoreCodeCamp.Data.Camp -> CoreCodeCamp.Models.CampModel
Type Map configuration:
Camp -> CampModel
CoreCodeCamp.Data.Camp -> CoreCodeCamp.Models.CampModel
Destination Member:
Talks
---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Talk -> TalkModel
CoreCodeCamp.Data.Talk -> CoreCodeCamp.Models.TalkModel
What am I doing wrong? I think the problem is related to the public ICollection<Talk> Talks { get; set; } property. Thanks in advance
Just add mapper between Talk and TalkModel like below:
public class CampProfile : Profile
{
public CampProfile()
{
this.CreateMap<Talk, TalkModel>();
this.CreateMap<Camp, CampModel>()
.ForMember(c => c.Venue, o => o.MapFrom(m => m.Location.VenueName))
//.ForMember(c => c.Talks, o => o.MapFrom(m => m.Talks))
.ReverseMap();
}
}
From the code you gave above, you need to config the map between Talk and TalkModel, check out Nested Mappings in AutoMapper.

How to Map (using AutoMapper) entities that have a ForeignKey in ASP.NET CORE 3.1.1 (C#,EntityFrameworkCore)

I have this function in my controller that creates a an entity:
[HttpPost]
[ProducesResponseType(typeof(ConnectionDBResponse), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ConnectionDBResponse>> PostConnectionDB([FromBody] CreateConnectionDBQuery query)
{
var connectionDBs = _mapper.Map<ConnectionDBDataModel>(query);
_context.ConnectionDB.Add(connectionDBs);
await _context.SaveChangesAsync();
var connectionDBResponse = _mapper.Map<ConnectionDBResponse>(connectionDBs);
return CreatedAtAction(nameof(GetAllConnectionDB), new { id = connectionDBs.Id }, connectionDBResponse);
}
For that I'm mapping between these two classes:
The response Class:
public class CreateConnectionDBQuery
{
public string ServerType { get; set; }
public string ServerName { get; set; }
public string port { get; set; }
public string AuthType { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string DBName { get; set; }
public string FolderName { get; set; }
public ScheduleConfigResponse ScheduleConfig { get; set; }
public Boolean hasEmails { get; set; }
public EmailConfigResponse EmailConfig { get; set; }
}
public class CreateScheduleConfigQuery
{
public string HourOfSave { get; set; }
public int NumDaysInDB { get; set; }
public CreateConnectionDBQuery ConnDB { get; set; }
public int ConnDBForeignKey { get; set; }
}
public class CreateEmailConfigQuery
{
public string SuccesEmail { get; set; }
public string FailureEmail { get; set; }
public CreateConnectionDBQuery ConnDB { get; set; }
public int ConnDBForeignKey { get; set; }
}
And the dataModel Class:
[Table("ConnectionDB")]
public class ConnectionDBDataModel
{
[Key]
public int Id { get; set; }
[Required]
public string ServerType { get; set; }
[Required]
public string ServerName { get; set; }
public string port { get; set; }
public string AuthType { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
[Required]
public string DBName { get; set; }
[Required]
public string FolderName { get; set; }
public ScheduleConfigDataModel ScheduleConfig { get; set; }
public Boolean hasEmails { get; set; }
public EmailConfigDataModel EmailConfig { get; set; }
}
[Table("ScheduleConfig")]
public class ScheduleConfigDataModel
{
[Key]
public int Id { get; set; }
public string HourOfSave { get; set; }
public int NumDaysInDB { get; set; }
public int ConnDBForeignKey { get; set; }
public ConnectionDBDataModel ConnDB { get; set; }
}
[Table("EmailConfig")]
public class EmailConfigDataModel
{
[Key]
public int Id { get; set; }
public string SuccesEmail { get; set; }
public string FailureEmail { get; set; }
public int ConnDBForeignKey { get; set; }
public ConnectionDBDataModel ConnDB { get; set; }
}
For that I'm using the AutoMapper as following:
#region ConnectionDB
CreateMap<ConnectionDBDataModel, ConnectionDBResponse>();
CreateMap<CreateConnectionDBQuery, ConnectionDBDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
CreateMap<UpdateConnectionDBQuery, ConnectionDBDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
#endregion
#region ScheduleConfig
CreateMap<ScheduleConfigDataModel, ScheduleConfigResponse>()
.ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());
CreateMap<CreateScheduleConfigQuery, ScheduleConfigDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());
CreateMap<UpdateScheduleConfigQuery, ScheduleConfigDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
#endregion ScheduleConfig
#region EmailConfig
CreateMap<EmailConfigDataModel, EmailConfigResponse>()
.ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());
CreateMap<CreateEmailConfigQuery, EmailConfigDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore());
CreateMap<UpdateEmailConfigQuery, EmailConfigDataModel>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
#endregion
But when I try to create this element it gives me an error saying that it's coming from an invalid Mapping as shown in the screen bellow:
I have tried to Ignore the Foreign Key (because my guess that this problem is coming from the foreignKey) using this line of code : .ForMember(dest => dest.ConnDBForeignKey, opt => opt.Ignore()); , but I guess it's not the way to solve that problem.
Any help would be appreciated thank you!
The error is happening because when mapping CreateConnectionDBQuery to ConnectionDBDataModel, there is no mapping defined for the types of the ScheduleConfig properties.
I'm guessing that in your CreateConnectionDBQuery, your ScheduleConfig property should be of type CreateScheduleConfigQuery instead of ScheduleConfigResponse.
Alternatively, if you don't want to change the models, you could add a mapping configuration from ScheduleConfigResponse to ScheduleConfigDataModel. But that doesn't seem very intuitive.

How to flattening child object used repeatedly in C# with Automapper

Using the following entities
public class User
{
public Guid Id { get; set; }
public string Username { get; set; }
}
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedByUser { get; set; }
public User DeletedByUser { get; set; }
}
How do I flatten this to the GeneralEntityDto below?
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
I have tried setting up my mappings as seen below but it fails with a complaint about "CreatedByUsername" and "DeletedByUsername" not being mapped.
protected void Configure()
{
CreateMap<GeneralEntity, GeneralEntityDto>()
.ForMember(dest => dest.CreatedByUsername,
opt => opt.MapFrom(src => src.CreatedByUser.Username))
.ForMember(dest => dest.DeletedByUsername, opt =>
opt.MapFrom(src => src.DeletedByUser.Username));
}
You can use the naming convention that automapper provides.
Basically if you include the exact string of the property name of the source Object you do not have to add ForMember() automapper is clever enough to do it automatically.
That means for example :
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedBy { get; set; } // renaming just for simplicity
public User DeletedBy { get; set; } // renaming just for simplicity
}
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
Reference also to these:
http://docs.automapper.org/en/stable/Flattening.html
AutoMapper TwoWay Mapping with same Property Name

Is there a way to do the mapping using Automapper

The TeacherSubjects list in TeacherVM always shows null even though Automapper is used to map SubjectVM to TeacherSubject.
I have tried the code below with the automapper configuration. SchoolName is pulling through but TeacherSubjectlist is always null.
public class Teacher
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public School WorkingSchool { get; set; }
public int SchoolId { get; set; }
public List<TeacherSubject> TeacherSubjectslist { get; set; }
}
public class TeacherSubject
{
public int TeacherSubjectId { get; set; }
public Subject Subject { get; set; }
public int SubjectId { get; set; }
public Teacher Teacher { get; set; }
public int TeacherId { get; set; }
}
public class TeacherVM
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public int SchoolId { get; set; }
public string SchoolName { get; set; }
public List<SubjectVM> TeacherSubjects { get; set; }
}
public class SubjectVM
{
public string SubjectName { get; set; }
public int SubjectId { get; set; }
}
CreateMap<domain.TeacherSubject, SubjectVM>()
.ForMember(dest => dest.SubjectName, opt => opt.MapFrom(src =>
src.Subject.SubjectName))
.ForMember(dest => dest.SubjectId, opt => opt.MapFrom(src =>
src.Subject.SubjectId));
CreateMap<domain.Teacher, TeacherVM>()
.ForMember(dest => dest.SchoolName, opt => opt.MapFrom(src =>
src.WorkingSchool.SchoolName))
.ForMember(dest => dest.TeacherSubjects, opt => opt.MapFrom(src
=> src.TeacherSubjectslist));
TeacherSubjectlist should be a list of the SubjectId and the SubjectName properties.
The problem is that you are missing creating instance of list types inside each class that contains list of objects.
What you need to do is add instance of list type in class constructor.
With out testing it, for example:
public Teacher()
{
TeacherSubjectslist = new List<TeacherSubject>();
..
..
The same concept is valid for the remaining classes that have list of objects.

Automapper complex object mapping NullReference exception

I have the following classes
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GroupTier> Tiers { get; set; }
}
public class GroupTier : IEntity
{
public int Id { get; set; }
public int GroupId { get; set; }
public int Tier { get; set; }
public decimal Amount { get; set; }
public virtual Group Group { get; set; }
}
I am trying to map to the following ViewModel
public class GroupViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<decimal> Tiers { get; set; }
}
using the configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am using EF6 to query from the database. I am having trouble when the Group.Tiers is null. How can I handle the null value?
When I use the this configuration
configuration.CreateMap<Group, GroupViewModel>()
.ForMember(m => m.Tiers, opt => opt.MapFrom(u => u.Tiers == null ? new List<decimal>() : u.Tiers.OrderBy(q => q.Tier).Select(q => q.Amount)));
I am getting this error
Cannot compare elements of type 'System.Collections.Generic.ICollection'

Categories