实体框架 - 添加带有代理对象的 POCO,然后更新同一对象会在数据库中创建重复的项目

发布于 2024-10-25 05:00:11 字数 1068 浏览 0 评论 0原文

我正在将 Entity Framework 4.0 与 POCO 实体结合使用,并使用动态代理进行更改跟踪。我正在尝试添加一个实体,调用 SaveChanges,然后更新一些需要从添加的对象返回 IDENTITY 值的其他实体。由于对象在 edmx 文件中不相关,因此我无法立即调用 SaveChanges 并让 EF 管理这一切。最后,我想编辑原始对象中的一些字段,然后让 SaveChanges 更新它。发生的情况是数据库正在为此原始项目创建重复记录。它正在添加第一个对象,返回一个新 ID,但是当我在编辑几个字段后调用 SaveChanges 时,它会再次将该项目添加到表中。有什么想法吗?

MyClass newItem = new MyClass();
newItem.field1 = 2;
newItem.field2 = false;
newItem.field3 = 44;

context.AddObject(newItem);
//force a save to get the identity of the added item so I can use it with another entity
context.SaveChanges();

//Setting the object state to unchanged (or not doing anything) will add a duplicate object
//If I set the object state to modified, it does not add a duplicate record, but it also does not save the updates
context.SetObjectState(newItem, ObjectStateEntry.Unchanged);

//do something else unrelated using the new ID returned from the IDENTITY field in the database

//update a couple of fields on the newly added item
newItem.field1 = 3;
newItem.field2 = true;
//save again
context.SaveChanges();

I am using Entity Framework 4.0 with POCO entities using dynamic proxies for change tracking. I am trying to add an entity, call SaveChanges, and then update some other entities that need the IDENTITY value returned from the added object. Since the objects are not related in the edmx file, I cannot call SaveChanges at once and have EF manage it all. And then at the end I want to edit a few fields in the original object and then have SaveChanges update it. What is happening is the database is creating duplicate records for this original item. It is adding the first object, returning a new ID, but then when I call SaveChanges later after editing a couple of fields, it is adding the item to the table again. Any ideas?

MyClass newItem = new MyClass();
newItem.field1 = 2;
newItem.field2 = false;
newItem.field3 = 44;

context.AddObject(newItem);
//force a save to get the identity of the added item so I can use it with another entity
context.SaveChanges();

//Setting the object state to unchanged (or not doing anything) will add a duplicate object
//If I set the object state to modified, it does not add a duplicate record, but it also does not save the updates
context.SetObjectState(newItem, ObjectStateEntry.Unchanged);

//do something else unrelated using the new ID returned from the IDENTITY field in the database

//update a couple of fields on the newly added item
newItem.field1 = 3;
newItem.field2 = true;
//save again
context.SaveChanges();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2024-11-01 05:00:11

这是我在调用 SetObjectState 时犯的一个错误。我有一个调用此方法的包装器,并且该包装器传递了错误的枚举值。现在一切正常。

This was a mistake on my part when calling SetObjectState. I had a wrapper that called this method, and the wrapper was passing in the wrong Enum value. Everything works okay now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文