[英]AutoMapper and manually mapping nested complex types
I have complex data, that is returned from my API as follows: 我有复杂的数据,这些数据是从我的API返回的,如下所示:
"id": 1002,
"user_id": "98fd8f37-383d-4fe7-9b88-18cc8a8cd9bf",
"organization_id": null,
"content": "A post with a hashtag!",
"created_at": "2018-05-25T21:35:31.5218467",
"modified_at": "2018-05-25T21:35:31.5218467",
"can_comment": true,
"can_share": true,
"post_tags": [
{
"post_id": 1002,
"tag_id": 1,
"tag": {
"id": 1,
"value": "hashtag",
"post_tags": []
}
}
]
That corresponds to my Post.cs
entity, and I created new model class to map it with, which looks like this: 对应于我的Post.cs
实体,我创建了新的模型类来映射它,如下所示:
public class PostModel
{
public int Id { get; set; }
[Required]
public string UserId { get; set; }
public int? OrganizationId { get; set; }
[Required]
public string Content { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
public bool CanComment { get; set; } = true;
public bool CanShare { get; set; } = true;
public List<Tag> Tags { get; set; }
}
Where my Tag.cs
and TagModel.cs
classes lookk like this: 我的Tag.cs
和TagModel.cs
类看起来像这样:
Tag.cs: Tag.cs:
public class Tag
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Value { get; set; }
public ICollection<PostTag> PostTags { get; set; } = new List<PostTag>();
}
TagModel.cs: TagModel.cs:
public int Id { get; set; }
[Required]
public string Value { get; set; }
So, basically, I have a mapping as 所以,基本上,我有一个映射为
CreateMap<Post, PostModel>()
.ReverseMap();
CreateMap<Tag, TagModel>()
.ReverseMap();
but what I get from the API once I do the mapping is this: 但是一旦完成映射,我从API中得到的是:
"id": 1002,
"user_id": "98fd8f37-383d-4fe7-9b88-18cc8a8cd9bf",
"organization_id": null,
"content": "Finally a post with a hashtag!",
"created_at": "2018-05-25T21:35:31.5218467",
"modified_at": "2018-05-25T21:35:31.5218467",
"can_comment": true,
"can_share": true,
"tags": null
So, as you can see the Tags
is not mapped. 因此,如您所见, Tags
未映射。 The reason is more likely that I have a many-to-many relationship between Post
and Tag
, and I originally get PostTags
from the API. 原因很可能是Post
和Tag
之间存在多对多关系,并且最初PostTags
从API获取PostTags
的。 How can I tell AutoMapper to map the tag
inside post_tags
to the corresponding tag inside PostModel
? 如何告诉AutoMapper将post_tags
的tag
post_tags
到PostModel
的相应标签?
AutoMapper maps the collection, not the objects in that collection, individually. AutoMapper分别映射集合,而不是该集合中的对象。 So, first ignore the collection and then handle it manually. 因此,首先忽略集合,然后手动处理它。
// Map the Post and ignore the Tags
AutoMapper.Mapper.CreateMap<Post, PostModel>()
.ForMember(dest => dest.Tags,
opts => opts.Ignore());
// Map the Tags and ignore the post_tags
AutoMapper.Mapper.CreateMap<Tag, TagModel>()
.ForMember(dest => dest.post_tags,
opts => opts.Ignore());
// Map the Post Model
AutoMapper.Mapper.Map(post, postModel);
// Map the tags
for (int i = 0; i < post.post_tags.Count(); i++)
{
AutoMapper.Mapper.Map(post.post_tags[i], postModel.Tags[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.