好吧我很难从模型转换到dto模型,反之亦然 该模型有效,但在尝试映射AutoMapper.dll中发生“AutoMapper.AutoMapperMappingException”时
这里是模型类
public class Category
{
public Guid CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public Guid PostId { get; set; }
public string PostTitle { get; set; }
public string PostMessage { get; set; }
public string PostImage { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public Guid Category_Id { get; set; }
[ForeignKey("Category_Id")]
public virtual Category Category { get; set; }
public virtual IdentityUser User { get; set; }
}
public class Tag
{
public Guid TagId { get; set; }
public string TagName { get; set; }
public virtual List<Post> Posts { get; set; }
}
这是我的DTO的
public class TagDto
{
public Guid TagId { get; set; }
public string TagName { get; set; }
public virtual IList<PostDto> Posts { get; set; }
}
public class PostDto
{
public Guid PostId { get; set; }
public string PostTitle { get; set; }
public string PostMessage { get; set; }
public string PostImage { get; set; }
public virtual ICollection<TagDto> Tags { get; set; }
[Required]
public Guid Category_Id { get; set; }
public virtual CategoryDto Category { get; set; }
public virtual IdentityUser User { get; set; }
}
这就是我一直在尝试使用automapper在实体之间进行映射
public static Tag Tag_ToModel(this TagDto dto)
{
AutoMapper.Mapper.CreateMap<TagDto, Tag>()
.ForMember(post => post.Posts, expr => expr.MapFrom(src => src.Posts))
.ConstructUsing(post => AutoMapper.Mapper.Map<Tag>(post.Posts));
var model = AutoMapper.Mapper.Map<Tag>(dto);
return model;
}
public static TagDto Tag_ToDto(this Tag model)
{
AutoMapper.Mapper.CreateMap<Tag, TagDto>()
.ForMember(post => post.Posts, expr => expr.MapFrom(src => src.Posts))
.ConstructUsing(post => AutoMapper.Mapper.Map<TagDto>(post.Posts));
var dto = AutoMapper.Mapper.Map<TagDto>(model);
return dto;
}
public static Post Post_ToModel(this PostDto dto)
{
AutoMapper.Mapper.CreateMap<PostDto, Post>()
.ForMember(prop => prop.Comments, expr => expr.MapFrom(src => src.Comments))
.ForMember(prop => prop.Tags, expr => expr.MapFrom(src => src.Tags))
.ConstructUsing(category => AutoMapper.Mapper.Map<Post>(category.Category))
.ConstructUsing(tag=>AutoMapper.Mapper.Map<Post>(tag.Tags));
var model = AutoMapper.Mapper.Map<Post>(dto);
return model;
}
public static PostDto Post_ToDto(this Post model)
{
AutoMapper.Mapper.CreateMap<Post, PostDto>()
.ForMember(prop => prop.Comments, expr => expr.MapFrom(src => src.Comments))
.ForMember(prop => prop.Tags, expr => expr.MapFrom(src => src.Tags)
.ConstructUsing(category => AutoMapper.Mapper.Map<PostDto>(category.Category))
.ConstructUsing(tag => AutoMapper.Mapper.Map<PostDto>(tag.Tags));
var dto = AutoMapper.Mapper.Map<PostDto>(model);
return dto;
}
public static Category Category_ToModel(this CategoryDto dto)
{
AutoMapper.Mapper.CreateMap<CategoryDto, Category>()
.ForMember(post => post.Posts, expr => expr.MapFrom(src => src.Posts));
var model = AutoMapper.Mapper.Map<Category>(dto);
return model;
}
public static CategoryDto Category_ToModel(this Category model)
{
AutoMapper.Mapper.CreateMap<Category, CategoryDto>()
.ForMember(post => post.Posts, expr => expr.MapFrom(src => src.Posts));
var dto = AutoMapper.Mapper.Map<CategoryDto>(model);
return dto;
}
我完全没有想法,也找不到任何有关如何映射此类的好教程。 我认为,例如在我的类别中,“帖子”没有按照应有的方式进行映射, 仍然“Post”还有其他一些导航道具,如IdentityUser,Tag,Comment和Category 而且所有这些实体都有导航道具。 这种配置是我的决定,但我一直在努力尝试别人,唯一一次工作就是 当我使用Ignore()作为成员时,但这意味着将不会映射thoes道具。 是否有任何具有此类问题且愿意提供帮助的自动化用户? 谢谢
答案 0 :(得分:0)
您应该在应用启动时从global.asax.cs或类似的东西调用createMap。在尝试执行任何地图操作之前,先设置所有映射一次。您需要设置所有映射,以便映射子属性,然后您不必为Posts等内容调用ForMember。
哦,摆脱构造使用。只需使用Mapper.Map。