AutoMapper不从相关的entites返回对象

时间:2015-10-01 06:16:48

标签: c# asp.net entity-framework-6 automapper automapper-4

我遇到了从相关域模型返回对象的问题。来自其他模型的对象返回null。

我基本上要完成的是返回一个DTO,它具有我想从相关域模型中获取的字段,而不是将每个字段直接从域模型传递到json。

请看下面的代码,有人可以提供建议。

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

2 个答案:

答案 0 :(得分:2)

AutoMapperTSource的属性映射到TDestination的属性,但它不会尝试通过TDestination的子属性查找TSource的属性默认值。

您可以指示AutoMapper执行此操作:

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

答案 1 :(得分:1)

AutoMapper还不是AI,因此您应该明确指定自定义成员映射:

var minimized_elements = $('.grid_moretext span');

    minimized_elements.each(function(){    
    var t = $(this).text();        
    if(t.length < 55) return;

    $(this).html(
    t.slice(0,55)+'<span>... </span>'+
    '<span class="more_text" style="display:none;">'+ t.slice(55,t.length)+' </span>'
    );
    }); 
    $('a.tr_expand').click(function(event){
        event.preventDefault();
        $(this).parent().siblings().find("span.more_text").toggle();       
                if ($(".tr_expand").text() == "More") {
                    $(".tr_expand").text("Less");
                }
                else {
                    $(".tr_expand").text("More");
                }
    });
    $('a.more', minimized_elements).click(function(event){
        event.preventDefault();
        $(this).hide().prev().hide();
        $(this).next().show();        
    });
    $('a.less', minimized_elements).click(function(event){
    event.preventDefault();
    $(this).parent().hide().prev().show().prev().show();    
    });

    $('a.expand_all').click(function(event){
    event.preventDefault();
    $(this).next().find(".grid_moretext span.more_text").toggle();  
        if ($(".expand_all").text() == "Expand All") {
            $(".expand_all").text("Collapse All");
        }
        else {
            $(".expand_all").text("Expand All");
        }
    });