Search code examples
c#asp.netentity-framework-6automapperautomapper-4

AutoMapper not returning objects from related entites


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.

SCREEN SHOT OF RESULT DATABASE DIAGRAM

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

Solution

  • 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);