使用实体框架自我跟踪实体进行更新
我遇到了一个我认为很容易解决的问题。 这是我的场景(实体框架 4 使用自我跟踪断开实体)。 假设我有 2 个实体:用户、订单。 从 asp.net 页面从数据库获取 1 个用户和 1 个订单。
const int userId = 1;
const int orderId = 1;
var userManager = new UserManager();
var orderManager = new OrderManager();
var user = userManager.GetUser(userId);
var order = channelManager.GetChannel(channelId);
user.Orders.Add(order);
现在我需要创建一个函数来更新向其中添加订单的用户。
我写了类似的内容:
public bool UpdateUser(User user)
{
context.AttachTo("Users", user);
var stateMgr = context.ObjectStateManager;
var stateEntry = stateMgr.GetObjectStateEntry(user);
for (int i = 0; i < stateEntry.CurrentValues.FieldCount; i++)
{
bool isKey = false;
string name = stateEntry.CurrentValues.GetName(i);
foreach (var keyPair in stateEntry.EntityKey.EntityKeyValues)
{
if (string.Compare(name, keyPair.Key, true) == 0)
{
isKey = true;
break;
}
}
if (!isKey)
{
stateEntry.SetModifiedProperty(name);
}
}
context.ApplyCurrentValues("Users", user);
return context.SaveChanges() > 0;
}
我在这个函数上没有任何错误,并且调试一切似乎都正常,但是当我检查数据库时,实体没有按预期更新。 我认为更新断开连接的实体很简单,但显然并非如此。 有人可以解释一下使用 EF4 更新断开连接的对象的整个图之间的逻辑吗?如果可以的话,请我需要理解逻辑,并且没有可供查看的链接集合。我已经花了一些时间在互联网上查找,但我发现了很多方法,我不确定哪一种是正确的。
谢谢
I'm having a problem that I thought was easy to resolve.
This is my scenario (Entity framework 4 disconnected entities using self tracking).
Let's say I have 2 entities: Users, Orders.
From an asp.net page a get from the database 1 user and 1 order.
const int userId = 1;
const int orderId = 1;
var userManager = new UserManager();
var orderManager = new OrderManager();
var user = userManager.GetUser(userId);
var order = channelManager.GetChannel(channelId);
user.Orders.Add(order);
Now I need to create a function that updates the user adding the order to it.
I wrote something like:
public bool UpdateUser(User user)
{
context.AttachTo("Users", user);
var stateMgr = context.ObjectStateManager;
var stateEntry = stateMgr.GetObjectStateEntry(user);
for (int i = 0; i < stateEntry.CurrentValues.FieldCount; i++)
{
bool isKey = false;
string name = stateEntry.CurrentValues.GetName(i);
foreach (var keyPair in stateEntry.EntityKey.EntityKeyValues)
{
if (string.Compare(name, keyPair.Key, true) == 0)
{
isKey = true;
break;
}
}
if (!isKey)
{
stateEntry.SetModifiedProperty(name);
}
}
context.ApplyCurrentValues("Users", user);
return context.SaveChanges() > 0;
}
I don't have any error on this function and debugging everything seems to be ok, but when I check on the database the entity is not updated as expected.
I thought update a disconnected entity was something simple but apparently is not.
Can someone explaing me the logic between the update the entire graph of disconnected object with EF4? Please if you can I need to undestand the logic and not have a collection of links to look at. I already spent some time looking on internet but I'm finding so many approches that I'm not sure which one is correct.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在您的代码中没有看到任何与自我跟踪实体相关的内容。无论如何,使用 ASP.NET 的 STE 效果不太好 。
你的代码应该做什么?看起来你想这样做:
但是它 不会保存关系。我刚刚回答了一些关于工作的相关问题与独立的图表。
I don't see anything related to Self tracking entities in your code. Anyway STEs with ASP.NET don't work very well.
What is your code supposed to do? It looks like you want to do this:
But it will not save relations. I just answered some related question about working with detached graphs.
对于自我跟踪实体,您必须使用 ApplyChanges() 方法来同步上下文上的更改(而不是附加)。
applychanges 将转到图表以更新链接的对象/集合。
刷新是可选的,这里只是为了推回数据库的最后一个值。顺便说一句,刷新不会更新链接的对象。
With Self Tracking Entities you have to use the ApplyChanges() method to sync the change on a context instead (and not attach).
The applychanges will go to the graph to update the linked object/collection.
The refresh is optionnal, it is here only to push back the last value of the database. By the way the refresh doesn't update the linked object.