Addto* - 删除对象 - addto* 将抛出错误
基本上我插入一行,删除该行,然后再次插入具有相同键的新行。它会在最后一行 savechanges 上抛出异常:“AcceptChanges 无法继续,因为该对象的键值与 ObjectStateManager 中的另一个对象冲突。在调用 AcceptChanges 之前,请确保键值是唯一的。”我确认删除对象调用有效。如何让objectstatemanager知道记录已经消失并且可以重新插入?
Maps _newMaps = new Maps();
_newMaps.map_page = "BLA";
_newMaps.descr = "BLA test";
opendb.AddToMaps(_newMaps);
opendb.SaveChanges(true);
foreach (var mapsrec in opendb.Maps)
{
opendb.DeleteObject(mapsrec);
}
opendb.SaveChanges(true);
Maps _sameMaps = new Maps();
_sameMaps.map_page = "BLA";
_sameMaps.descr = "BLA test";
opendb.AddToMaps(_sameMaps);
opendb.SaveChanges(true);
Basically I insert a row, delete that row, then insert a new row with the same key again. It will throw an exception on the last line savechanges: "AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges." I confirmed the deleteobject call works. How do I let the objectstatemanager know that the record is gone and can be reinserted?
Maps _newMaps = new Maps();
_newMaps.map_page = "BLA";
_newMaps.descr = "BLA test";
opendb.AddToMaps(_newMaps);
opendb.SaveChanges(true);
foreach (var mapsrec in opendb.Maps)
{
opendb.DeleteObject(mapsrec);
}
opendb.SaveChanges(true);
Maps _sameMaps = new Maps();
_sameMaps.map_page = "BLA";
_sameMaps.descr = "BLA test";
opendb.AddToMaps(_sameMaps);
opendb.SaveChanges(true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进一步的研究表明,您必须 Detach() 添加到状态管理器的对象(记录),才能真正忘记它,即使在执行了 DeleteObject() 之后也是如此。因此,允许您添加记录、删除记录、使用相同键添加记录的代码如下所示:
Further research shows that you have to Detach() the object (record) you added to the state manager for it to truly forget about it even after doing a DeleteObject(). So code that would let you add a rec, delete it, add record with same keys would look like: