Reputation: 117
I am receiving PostDTO object in the controller and I am mapping it to Post entity and updating database. The problem occurs because PostDTO doesn't have Status property and Post entity does have Status property, so when I map PostDTO to Post, Post.Status becomes null. I don't want it to be null, I want it to stay unaffected in database. I could retrieve post from database and manually map Status property but is there better solution for this?
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
Upvotes: 1
Views: 1240
Reputation: 38394
You could either use an approach like this to ensure the field isn't updated: https://stackoverflow.com/a/10271910/84206
OR you can first GET the entity from the database, so that the Status is populated from the database, then apply the DTO changes to the entity.
There's other approaches, but they can't really be applied with EF.
Upvotes: 2