【发布时间】:2021-01-11 15:54:11
【问题描述】:
我在控制器中接收 PostDTO 对象,并将其映射到 Post 实体并更新数据库。出现问题是因为 PostDTO 没有 Status 属性,而 Post 实体确实具有 Status 属性,所以当我将 PostDTO 映射到 Post 时,Post.Status 变为 null。我不希望它为空,我希望它在数据库中不受影响。我可以从数据库中检索帖子并手动映射 Status 属性,但有更好的解决方案吗?
public class PostDTO
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public bool Urgent { get; set; }
}
public class Post
{
public long Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public bool Urgent { get; set; }
public string Status { get; set; }
}
var post = _mapper.Map<Post>(postDto); //here post.Status becomes null
【问题讨论】:
标签: entity-framework automapper dto