问题描述
我有一些困惑,从这个的关于修改分离对象。
I have some confusion, stemming from this http://msdn.microsoft.com/en-us/library/vstudio/bb896248(v=vs.100).aspx regarding modifying a detached object.
这似乎表明,如果我处于分离状态的修改一个对象,当我重新安装,我应该使用,而不是ApplyCurrentValues ApplyOriginalValues。然而,当我这样做,它似乎永远不会更新对象作为例子,除非我重新连接后修改的对象。
但是如果我附上修改后,那么它似乎并不重要,其中这些我使用(applyoriginal或applycurrent)
That seems to indicate if I modify an object in a detached state, when I reattach, I should use ApplyOriginalValues instead of ApplyCurrentValues. However, when I do this, it seems to never update the object as in the example, unless I modify the object after reattaching it.However if I modify after attaching, then it doesn't seem to matter which of these I use (applyoriginal or applycurrent).
下面是我的代码:
//this never works
private void UpdateWithOriginal(Category cat, string name)
{
using (TestContext ctx = new TestContext())
{
cat.Name = name;
ctx.Categories.Attach(cat);
ctx.ApplyOriginalValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(cat).State; //never modified state here
ctx.SaveChanges();
}
}
//this always works
private void UpdateWithCurrent(Category cat, string name)
{
using (TestContext ctx = new TestContext())
{
ctx.Categories.Attach(cat);
cat.Name = name;
ctx.ApplyCurrentValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(cat).State;
ctx.SaveChanges();
}
}
有谁知道为什么MSDN链接似乎表明,该//这永远不会奏效,位应该工作?
Does anyone know why the MSDN link seems to indicate that the //this never works, bit should work?
推荐答案
这结束了是那么容易。我不知道为什么我不明白。这里是解决这个:
This ended up being so easy. I don't know why I didn't understand it. Here is the solution to this:
private void UpdateWithOriginal(Category cat, string name)
{
Category updatedObject = new Category()
{
CategoryID = cat.CategoryID,
Name = cat.Name,
Topics = cat.Topics
};
updatedObject.Name = name;
using (TestContext ctx = new TestContext())
{
ctx.Categories.Attach(updatedObject);
ctx.ApplyOriginalValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(updatedObject).State; //never modified state here
ctx.SaveChanges();
}
}
我缺少这是什么。你有两个对象,一个原始和更新的。您分离,发送一个开了一个WCF服务,更改,对象被送回,您的最终重新创建对象作为一个更新的对象。现在,为了更新您的背景下,你需要两个对象,因为与给定的代码,如果你连接到你的上下文updatedObject,你的实体的状态是这样的:
What I was missing is this. You have two objects, an original and an updated. You detach, send one off to a WCF service, changes are made, object is sent back, on your end you recreate the object as an updated object. Now in order to update your context, you need two objects, because with the given code, if you attach to your context the updatedObject, the status of your entity is this:
- 原始值时─名称=鲍勃
- 当前值时─名称=鲍勃
有没有不同,所以.SAveChanges()不会做任何事情。既然你attatched更新的对象,您必须使用ApplyOriginalVAlues(类别,OriginalCategory)造成的:
There is no different, so .SAveChanges() won't do anything. Since you attatched the updated object, you MUST use ApplyOriginalVAlues("Categories",OriginalCategory) to cause this:
- 初始值:名称=史蒂夫
- 当前值:名称=鲍勃
现在你有修改的对象,当你打电话.SaveChanges()的更改将生效。这样做的正好相反,如果你附上原始对象(你将需要不修改原有的价值,但当前值,让你用ApplyCurrentVAlues())。
Now you have a modified object and when you call .SaveChanges() the change will take effect. The reverse of this is true if you attach the original object (you will need to not modify the original value, but the current value, so you use ApplyCurrentVAlues()).
这篇关于实体框架修改独立对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!