0

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

我基本上想要完成的是返回一个 DTO,其中包含我想要从相关域模型中获得的字段,而不是将每个字段直接从域模型传递给 json。

请参阅下面的代码,有人可以建议。

结果的屏幕截图 数据库图

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

2 回答 2

2

AutoMapper将 的属性映射TSource到 的属性,但默认情况下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);
于 2015-10-01T06:35:26.797 回答
1

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

Mapper.CreateMap<CourseDomainModel, CourseDTO>()
                .ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.TutorDomainModel.Email));
于 2015-10-01T06:32:54.300 回答