I'm experiencing a pretty weird issue i can't explain. I can create a new entity and link it to his foreign key, but i can't update. This is my code
My Post.cs Entity :
public class Post(){
public int Id {get;set;}
public string Title {get;set;}
...
public int PostCategoryId {get;set;}
public virtual Category Category {get;set;}
}
My Category.cs Entity :
public class Category(){
public int Id {get;set;}
public string Title {get;set;}
...
public virtual IList<Post> Posts {get;set;}
}
The Update Method in the controller :
[HttpPost]
public ActionResult EditPost(Post post) {
....
_repository.UpdatePost(post);
And the UpdatePost Method in the repository :
public void UpdatePost(Post post){
var category = _db.Category.SingleOrDefault(x => x.Id == post.PostCategoryId);
post.Category = category;
// I clearly see that the post have the category in the object
_db.Entry(post).State = EntityState.Modified;
_db.SaveChanges();
Every property of my object is updated, unless the Category.
Does anyone have a solution ?
Thank you so much, i can't sleep 'cause of that issue :)