AutoMapper 不从相关实体返回对象
AutoMapper not returning objects from related entites
我在 return 从相关域模型中获取对象时遇到问题。来自其他模型的对象 returning null。
我基本上想要完成的是 return 一个 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;
}
AutoMapper 还不是 AI,因此您应该明确指定自定义成员映射:
Mapper.CreateMap<CourseDomainModel, CourseDTO>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.TutorDomainModel.Email));
AutoMapper
将 TSource
的属性映射到 TDestination
的属性,但它不会尝试从 TSource
的子属性中查找 TDestination
的属性] 默认。
您可以指示 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);
我在 return 从相关域模型中获取对象时遇到问题。来自其他模型的对象 returning null。
我基本上想要完成的是 return 一个 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;
}
AutoMapper 还不是 AI,因此您应该明确指定自定义成员映射:
Mapper.CreateMap<CourseDomainModel, CourseDTO>()
.ForMember(dest => dest.Email, opt => opt.MapFrom(source => source.TutorDomainModel.Email));
AutoMapper
将 TSource
的属性映射到 TDestination
的属性,但它不会尝试从 TSource
的子属性中查找 TDestination
的属性] 默认。
您可以指示 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);