简体   繁体   English

实体框架6-一对多关系不想更新,但是可以创建作品

[英]Entity Framework6 - One to many relationship doesn't want to update, but create works

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 : 我的Post.cs实体:

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 : 我的Category.cs实体:

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 : 还有仓库中的UpdatePost方法:

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 :) 非常感谢您,由于这个问题,我无法入睡:)

It might be that your Category variable is named Category, which is the exact same name as the class itself. 可能是您的Category变量命名为Category,它与类本身的名称完全相同。 Try renaming it to category (lowercase) or to PostCategory and see if that helps. 尝试将其重命名为类别(小写)或PostCategory,看看是否有帮助。 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. EntityState通常用于直接从视图模型带来更改的情况,而不是在手动更改对象的字段之后。

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. 我仍然会删除EntityState行。

Also, I'm assuming you're just trying to update the SQL table's "PostCategoryId" entry? 另外,我假设您只是在尝试更新SQL表的“ PostCategoryId”条目? 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();

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). Matt S.,也许我错了,但是,如果我从数据库获取帖子,那么我将不得不更新可能由用户更新的每个字段(实际上,Post实体中有10个属性)。 And, the PostCategoryId is uptaded, any issue about that field. 并且,PostCategoryId已更新,有关该字段的任何问题。 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. 但是我认为我可以将Category更新为“发布到帖子中”,并在需要时调用post.Category。 Maybe it's a misunderstanding from me ? 也许这是我的误会?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM