这些是我的Entities
:在这种情况下,每个用户都可以创建一个Post
。每个帖子可以有一个Items
列表,每个项目都可以有一个带有标签的用户列表。
public class Post
{
public int Id { get; set; }
public int UserId { get; set; }
public string Body { get; set; }
public int LikeCount { get; set; }
public PostStatuses Status { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
public List<PostItem> PostItems { get; set; }
public Post()
{
PostItems = new List<PostItem>();
}
}
public class PostItem
{
public int Id { get; set; }
public int PostId { get; set; }
public long AttachmentId { get; set; } // Refer to CDN file Id
[ForeignKey("PostId")]
public Post Post { get; set; }
public List<PostItemTag> TaggedUsers { get; set; }
}
public class PostItemTag
{
public int PostItemId { get; set; }
public int TaggedUserId { get; set; }
public int X { get; set; }
public int Y { get; set; }
[ForeignKey("PostItemId")]
public PostItem PostItem { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
}
这些是我的DTO
:
public class CreatePostRequestDTO
{
public int UserId { get; set; }
public string Body { get; set; }
public List<PostItemDTO> PostItems { get; set; }
}
public class PostItemDTO
{
public long AttachmentId { get; set; }
public List<PostItemTagDTO> TaggedUsers { get; set; }
}
public class PostItemTagDTO
{
public int UserId { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
这是我的API:
public async Task<BaseResponseDTO<bool>> CreatePost(CreatePostRequestDTO createPostRequestDTO)
{
Post post = new Post()
{
Body = createPostRequestDTO.Body,
Status = PostStatuses.Posted,
UserId = createPostRequestDTO.UserId,
};
post.Mentions = createPostRequestDTO.Mentions.Select(x => new PostMention()
{
Post = post,
UserId = x,
}).ToList();
post.PostItems = createPostRequestDTO.PostItems.Select(x => new PostItem()
{
AttachmentId = x.AttachmentId,
Post = post,
PostAttachmentType = x.PostAttachmentType,
TaggedUseres = x.TaggedUseres.Select(c => new PostItemTag()
{
UserId = c.UserId,
X = c.X,
Y = c.Y,
PostItemId = ? // My problem is here.
}).ToList(),
}).ToList();
...
}
我该如何解决?
编辑: 一些多余的代码已被删除。
Edit2: 并且还删除了一些额外的说明。
答案 0 :(得分:1)
对于与Stores : {
Name:
Countries : {
Name:
States : {
Name:
}
}
的模型绑定,请执行以下步骤:
Automapper
和AutoMapper
AutoMapper.Extensions.Microsoft.DependencyInjection
添加到services.AddAutoMapper(typeof(Startup));
像
一样添加Startup.cs
ModelProfile.cs
UseCase
public class ModelProfile: Profile
{
public ModelProfile()
{
CreateMap<CreatePostRequestDTO, Post>();
CreateMap<PostItemDTO, PostItem>();
CreateMap<PostItemTagDTO, PostItemTag>();
}
}