【问题标题】:Using Automap to map ViewModel to Entity in many-to-many relationship使用 Automap 以多对多关系将 ViewModel 映射到 Entity
【发布时间】:2019-05-24 19:21:52
【问题描述】:

我有两个实体,它们的关系是多对多的。代码如下:

public class Post : Entity
{
    //public int Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime CreateTime { get; set; }
    public DateTime LastModify { get; set; }

    public ICollection<PostTag> PostTags { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}
public class Tag :Entity
{
    //public int Id { get; set; }
    public string TagName { get; set; }

    public ICollection<PostTag> PostTags { get; set; }
}
public class PostTag :Entity
{
    //public int Id { get; set; }

    public int PostId { get; set; }
    public Post Post { get; set; }
    public int TagId { get; set; }
    public Tag Tag { get; set; }
}
public class PostViewModel
{
    public string Title { get; set; }
    public string Body { get; set; }
    public DateTime CreateTime { get; set; }
    public DateTime LastModify { get; set; }
    public int AuthorId { get; set; }
    public string AuthorName { get; set; }
    public List<TagViewModel> Tags { get; set; }
}

现在,我已经创建了从实体到视图模型的映射配置:

CreateMap<Post, PostViewModel>()
    .ForMember(dest => dest.AuthorId, opt => opt.MapFrom(src => src.Author.Id))
    .ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.PostTags))
    .ForMember(dest => dest.AuthorName, opt => opt.MapFrom(src => src.Author.User.LastName))
    .ForMember(dest => dest.Body, opt => opt.MapFrom(src => src.Body))
    .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
    .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.CreateTime))
    .ForMember(dest => dest.LastModify, opt => opt.MapFrom(src => src.LastModify))
    .ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.PostTags.Select(y => y.Tag).ToList()));

我的问题是,当我收到类似以下的 PostViewModel 时:

public async Task<IActionResult> Post([FromBody] PostViewModel postViewModel)
{

}

如何将 PostViewModel 映射到 Post?我想从 PostViewModel 创建一个 Entity 并将它们添加到 DbContext 中,即 post、tag(如果不存在)和 postTag 关系,我该怎么办?

我对 automapper 很陌生,这是我的第一个问题,我希望我做对了,非常感谢您的回答和指导。

【问题讨论】:

  • 检查 Automapper.Collection。
  • @LucianBargaoanu 你能举个例子吗?
  • 没有,但是很多人这样做,所以应该很容易找到。
  • @LucianBargaoanu 还是谢谢你,我已经弄明白了。

标签: entity-framework-core automapper


【解决方案1】:

我认为你有两个选择。

1) Reverse mapping

您可以将.ReverseMap() 添加到映射配置的末尾

CreateMap<Post, PostViewModel>()
    .ForMember(dest => dest.AuthorId, opt => opt.MapFrom(src => src.Author.Id))
    ...
    .ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.PostTags.Select(y => y.Tag).ToList()))
    .ReverseMap();

2)您可以为PostViewModel定义一个新的配置Post

CreateMap<PostViewModel, Post>()
    .ForMember(dest => dest.Author.Id, opt => opt.MapFrom(src => src.AuthorId))
    ...etc

然后Mapper.Map&lt;Post&gt;(postViewModel)

【讨论】:

  • 我可以对否决票发表评论吗?这样一来我可以修改我的答案,二来让大家学习。
猜你喜欢
  • 2011-07-01
  • 2021-12-19
  • 2014-08-17
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
相关资源
最近更新 更多