银光+ WCF 数据服务获取 InvalidOperationException:上下文已在跟踪具有相同资源 Uri 的不同实体

发布于 2024-10-07 10:50:31 字数 1424 浏览 1 评论 0原文

我正在尝试用新对象替换一个对象,但遇到了上述异常。我尝试了几种组合,但无法绕过它。

我有一个包含项目的播放列表(项目有另一个嵌套对象,但我将其保留以帮助使我的问题更清晰。用户可以更改播放列表中的项目。

 if (playlistChanged)
{
    // remove selectedForRemoval
    IEnumerable<PlaylistItemViewModel> nonSelectedItems = selectedDisplayTemplates.Where(pivm => pivm.IsSelectedForRemoval);
    foreach (temViewModel ivm in nonSelectedItems)
    {
        context.DeleteObject(ivm.Model);        
    }

    // clear out and remove old items
    foreach (Item item in playlist.PlaylistItems)
    {
        context.DeleteObject(item);
    }

    playlist.PlaylistItems.Clear();

    // add the selectedItem(s) to the playlist
    // these items can be from the Media, or other tables
    // so are newly created in code on the client           
    foreach (ItemViewModel ivm in selectedItems)
    {
        playlist.Items.Add(ivm.PlaylistItemModel);
        context.AddToItems(ivm.PlaylistItemModel);
    }
    context.BeginSaveChanges(SaveChangesOptions.Batch, new AsyncCallback((iar) =>
    {
        try
        {
            // Callback method for the async request, retrieves the status of the requested action
            DataServiceResponse response = context.EndSaveChanges(iar);
        }
        catch (DataServiceRequestException)
        {
            throw;
        }
    }), context);   
}

感谢任何帮助。

编辑:我正在覆盖Playlist 部分类中的 Equals 和 ToString 删除后,它开始工作。

I'm trying to replace an object with a new one and am getting the mentioned exception. I've tried several combination and can't get around it.

I have a Playlist that has Items (the Items has another nested object, but I'm leaving it out to help make my question clearer. The user can change which items are in the playlist.

 if (playlistChanged)
{
    // remove selectedForRemoval
    IEnumerable<PlaylistItemViewModel> nonSelectedItems = selectedDisplayTemplates.Where(pivm => pivm.IsSelectedForRemoval);
    foreach (temViewModel ivm in nonSelectedItems)
    {
        context.DeleteObject(ivm.Model);        
    }

    // clear out and remove old items
    foreach (Item item in playlist.PlaylistItems)
    {
        context.DeleteObject(item);
    }

    playlist.PlaylistItems.Clear();

    // add the selectedItem(s) to the playlist
    // these items can be from the Media, or other tables
    // so are newly created in code on the client           
    foreach (ItemViewModel ivm in selectedItems)
    {
        playlist.Items.Add(ivm.PlaylistItemModel);
        context.AddToItems(ivm.PlaylistItemModel);
    }
    context.BeginSaveChanges(SaveChangesOptions.Batch, new AsyncCallback((iar) =>
    {
        try
        {
            // Callback method for the async request, retrieves the status of the requested action
            DataServiceResponse response = context.EndSaveChanges(iar);
        }
        catch (DataServiceRequestException)
        {
            throw;
        }
    }), context);   
}

Any help is appreciated.

EDIT: I was overriding the Equals and ToString in Playlist partial class. After I removed those, it started working.

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

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

发布评论

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

评论(2

高冷爸爸 2024-10-14 10:50:31

我在 Silverlight 中重写了 Playlist 部分类中的 Equals 和 ToString。当我删除这些后,它就开始工作了。从现在起我将使用 WCF 数据服务来避免这种情况。

I was overriding the Equals and ToString in Playlist partial classes in Silverlight. After I removed those, it started working. I'm going to avoid that from now on with WCF Data Services.

雨巷深深 2024-10-14 10:50:31

如果您使用与尝试添加/删除的上下文不同的上下文来获取数据,您将收到您发布的异常。要么处理您获取数据的原始上下文,要么在您调用 AddItem/DeleteObject 的项目上显式调用 Detach。

If you fetched the data using a different context from the one you are trying to add/delete with, you will get the exception you posted. Either dispose of the original context you fetched the data with or explicitly call Detach on the item you are calling AddItem/DeleteObject on.

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