ObjectSet.ApplyCurrentValues v/s StateManager.ChangeObjectState
我对实体框架 4.1 很陌生。我开始使用 EF 作为我的 DAL 编写一个新应用程序。我正在使用 POCO 类(使用 POCO t4 模板)和数据库优先方法。
我的 GenericRepository 具有以下 Update 方法
public void Update(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
//_objectSet.ApplyCurrentValues(entity);
}
我有一个 gridview,它与具有 UpdateMethod="UpdateStore" 的 ObjectDataSource 进行数据绑定,该 UpdateMethod="UpdateStore" 仅采用一个参数,如下...
public void UpdateStore(Store franchise)
{
unitOfWork.StoreRepository.Attach(franchise);
unitOfWork.StoreRepository.Update(franchise);
unitOfWork.SaveChanges();
}
我在这里遇到了几个问题:
当我看到 < 的文档时a href="http://msdn.microsoft.com/en-us/library/bb896271.aspx" rel="nofollow">ApplyCurrentValues,它证明我应该使用这个更新值,但这不起作用(正如您在上面的注释行中看到的那样)。然后,当我尝试使用 ObjectStateManager.ChangeObjectState 时,它起作用了。这两种方法有什么区别。
如您所见,我先附加,然后应用更新方法。我可以在 GenericRepository 更新方法中结合附加和更新状态吗?这样做有什么陷阱吗?
I am quite new to Entity Framework 4.1. I am starting to write a new application using EF as my DAL. I am using POCO classes (using POCO t4 template) with database first approach.
My GenericRepository has got below Update method
public void Update(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
//_objectSet.ApplyCurrentValues(entity);
}
I having a gridview which is databind to ObjectDataSource having UpdateMethod="UpdateStore" which takes only one parameter is as follows...
public void UpdateStore(Store franchise)
{
unitOfWork.StoreRepository.Attach(franchise);
unitOfWork.StoreRepository.Update(franchise);
unitOfWork.SaveChanges();
}
I got couple of question here:
When I see the documentation for ApplyCurrentValues, it proves me that I should use this to update values but this doesn't work (as you can see in above commented line). Then when I try with ObjectStateManager.ChangeObjectState, it works. What's the difference between this two approaches.
As you can see, I attach first and then apply the update method. Can I combine attach and updating state inside my GenericRepository update method. Is there any pitfall there in doing so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ObjectDataSource
接收的分离实体的值时,请使用ApplyCurrentChanges
。当您执行此操作时,所有新值都会复制到附加实体,并自动标记为已修改
。ChangeObjectState
用于更改附加实体的状态 - 它不执行任何其他操作。调用Attach
会附加处于Unchanged
状态的实体,因此这就是为什么您必须更改状态才能将更改保留在数据库中的原因。Id
的实体将会抛出异常。ApplyCurrentChanges
is used when you have attached entity loaded from the database and you want to update its values from detached entity received from yourObjectDataSource
. When you do this all new values are copied to attached entity and it is automatically marked asModified
.ChangeObjectState
is used to change state of attached entity - it does nothing else. CallingAttach
attaches entity inUnchanged
state so it is the reason why you must change the state to get your changes persisted in the database..Id
twice will throw exception.