1

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 :)

Loot
  • 71
  • 8

2 Answers2

0

It might be that your Category variable is named Category, which is the exact same name as the class itself. Try renaming it to category (lowercase) or to PostCategory and see if that helps. Or it might be an issue with the entity state. EntityState is usually for when you bring changes directly from the view model, not after manually making changes to an object's fields.

Actually I think I know what it is. You need to fetch the Post from the database, not the Category.

Category category = _db.Category.SingleOrDefault(x => x.Id == post.PostCategoryId);
Post p = db.Post.SingleOrDefault() //whatever code you need to find the Post in the database
p.Category = category;
db.SaveChanges();

I would still remove the EntityState line.

Also, I'm assuming you're just trying to update the SQL table's "PostCategoryId" entry? In that case all you need to do is this:

Post p = db.Post.SingleOrDefault() //whatever code you need to find the Post in the database
p.PostCategoryId = post.PostCategoryId
db.SaveChanges();
  • Hello Matt S, I didn't explained every solutions i've tried, but yeah, i tried to change the Property name to PostCategory and it didn't work. And the Post Entity has clearly the right Cateogry when i had it. Do you think that should works if the variable name was different ? To be honest i really don't know. I'll try, thank you :) – Loot Jul 09 '15 at 12:42
  • Alright. I made an edit to my answer so make sure you read that too. –  Jul 09 '15 at 12:45
  • Ok, thank you for your feedback, i'll try to apply your advice tonight and tell you what it does. Thank you – Loot Jul 09 '15 at 12:48
  • Maybe i'm wrong but, if i get the post from the db, i'll have to update every field that may be updated from the user (in fact, there is 10 properties in the Post entity). And, the PostCategoryId is uptaded, any issue about that field. Tell me if i'm wrong, but i would have to avoid to request for the Category but get it on the post. Like : : – Loot Jul 09 '15 at 12:57
  • So there's no Category field in your database, correct? Then if PostCategoryId is being updated, that means it's working properly. When you want to get a post's category, you can just use the PostCategoryId field to look up the category. Or, EntityFramework should be able to detect that the PostCategoryId field is a foreign key for a Category object. –  Jul 09 '15 at 13:06
  • Ok, that's what i was thinking about. It's like EF doesn't see that PostCategoryId is a foreignKey. And I tried the annotation, for example, to clearly show that it is, but maybe i should take attention on the foreign key point – Loot Jul 09 '15 at 13:13
0

Matt S., Maybe i'm wrong but, if i get the post from the db, i'll have to update every field that may be updated from the user (in fact, there is 10 properties in the Post entity). And, the PostCategoryId is uptaded, any issue about that field. Tell me if i'm wrong, but i would have to avoid to request for the Category but get it on the post. Like :

var post.Category = _db.Category.SingleOrDefault(x => x.Id == post.CategoryId) 

I could create a method for that and call it every time i need to retrive the Category Entity related to the Post.

But i thought i could do update the Category "into the post" and just call post.Category when I need. Maybe it's a misunderstanding from me ?

Loot
  • 71
  • 8