简体   繁体   English

实体框架4(纯POCO)上的UPDATE操作问题

[英]Problem with UPDATE Operation On Entity Framework 4 (Pure POCOs)

I have an Entity Framework 4.0 model implemented with pure POCO's (no code generation, no self-tracking entities, just plain old CLR objects). 我有一个使用纯POCO实现的Entity Framework 4.0模型(没有代码生成,没有自我跟踪实体,只是普通的旧CLR对象)。

Now, here is some code i have in my UI to perform an UPDATE: 现在,这是我在UI中执行更新的一些代码

[HttpPost]
public ActionResult UpdatePerson(Person person)
{
    repository.Attach(person);
    unitOfWork.Commit();
}

Essentially, i have an Action Method which accepts a strongly-typed Person object, and i need to UPDATE this entity. 本质上,我有一个接受强类型Person对象的Action Method,并且我需要更新此实体。

Doesn't error, but also doesn't persist the changes to the DB either. 不会出错,但是也不会将更改持久保存到数据库中。 :( :(

When i inspect the EntityState during the "Commit" of my Unit of Work: 当我在工作单元的“提交”期间检查EntityState时:

var entities = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);

I see my entity, with EntityState.Unchanged . 我看到了带有EntityState.Unchanged的实体。

So that explains why it has not been persisted. 因此,这说明了为什么它没有一直存在。 My Find,Add,Delete operations are working fine (persisting correctly). 我的“查找”,“添加”,“删除”操作工作正常(持续存在)。 But UPDATE doesn't seem to work. 但是UPDATE似乎无效。

I've found some threads saying i need to manually set the object state: 我发现一些线程说我需要手动设置对象状态:

ctx.ObjectStateManager.ChangeObjectState(person, EntityState.Modified);

Is that correct? 那是对的吗? Where would i put this logic? 我该把逻辑放在哪里? Expose as an operation on my Unit of Work? 公开作为我的工作单位的一项操作?

Obviously the issue is i have no change tracking whatsoever, due to the use of Pure POCO's (no EntityObject derivation, no INotifyPropertyChanging implementation). 显然,问题是由于使用了纯POCO(没有EntityObject派生,没有INotifyPropertyChanging实现),我也没有任何变化来跟踪跟踪

But i haven't found an issue with it until now. 但是直到现在我还没有发现任何问题。

What am i doing wrong? 我究竟做错了什么?

Well, since you don't have any change tracking, etc., there should be a way for EF to determine what to do with the attached entity when SaveChanges() is called. 好吧,由于您没有任何更改跟踪等功能,因此应该有一种方法可以让EF确定在调用SaveChanges()时如何处理附加的实体。 By default, the EntityState is Unchanged, so if you want to Update, you have to manually set the state. 默认情况下,EntityState是未更改的,因此,如果要更新,则必须手动设置状态。

Another way would be to query for the Person with this Id, rewrite the properties with the data you fetch from the controller, and call SaveChanges() . 另一种方法是使用此ID查询Person ,使用您从控制器获取的数据重写属性,然后调用SaveChanges() This is a safer way actually, since you can guard yourself from a situation where the Person gets deleted while the user is editing the webform; 实际上,这是一种更安全的方法,因为您可以防止在用户编辑Web表单时删除Person的情况; but this obviously requires an extra roundtrip to the DB, which is often less desirable. 但这显然需要与数据库进行额外的往返,这通常不太理想。

In any case, I would have a repository Update() method (more specific than Attach ), that would actually attach the entity and change the EntityState to Modified . 无论如何,我都会有一个存储库Update()方法(比Attach更具体),该方法实际上将附加实体并将EntityState更改为Modified And you still have relatively clean code in your Action: 而且您的Action中仍然有相对干净的代码:

public ActionResult UpdatePerson(Person person)
{
    repository.Update(person);
    unitOfWork.Commit();
}

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

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