WPF克隆/分离对象编辑问题 - 什么是标准? [英] WPF Cloned/Detached object edit problem - what is the standard?

查看:172
本文介绍了WPF克隆/分离对象编辑问题 - 什么是标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个ObservableCollection来包装一些我生成的实体框架对象。
当用户想要编辑一些值时,我打开一个包含字段的弹出窗口,当用户更改并按保存时 - 更改将保存到数据库中,绑定的控件是一个更改,因为它是observablecollection。

I am using an ObservableCollection to wrap some of my generated entity framework objects. When the user wants to edit some values , i am opening a popup windows that contains fields, and when the user changes and press save - the changes are saved to the database, and the binded controls are changes as it is an observablecollection.

为了防止用户在同一个绑定对象上工作(同时导致每个绑定控件的视觉变化),我想使用一些功能克隆对象,然后分离原来的,附加克隆的对象,并将其保存到数据库。问题是克隆对象不能正确保存到数据库。如果我只尝试分离对象,编辑然后附加 - 当分离时,它会丢失所有的父项和小孩参考...

To prevent the user from working on the same binded object (it causes the visual change of every binded control on the same time) i want to use some functionality the clone the object, and then detach the original, attach the cloned object, and save it to the database. The problem is that the cloned object does not save properly to the database. If i try only to detach the object, edit and then attach - when detached it loses all its parent and child refernces...

WPF中的CRUD标准是什么?如何清理当前行,并保留在ObservableCollection?

What is the CRUD standard in WPF? How can i cleanly edit a current row, while keeping it in an ObservableCollection?

请帮助....

Oran

推荐答案

好吧,我找到了一个很好的解决方案。

Well it seems that i have found a fine solution.


  1. 首先实现可克隆对象容器:

  1. First implement your cloneable object container :

public class ClonableObjectContainer : Object , ICloneable
{
    private Object data;

    public ClonableObjectContainer(Object obj)
    {
        data = obj;
    }

    public Object Data
    {
        get { return data; }
    }

    public object Clone()
    {
        return (ClonableObjectContainer)this.MemberwiseClone();
    }
}


  • 然后用它的克隆方法创建分离的编辑对象:

  • Then use this object with its Clone method to create your detached edit object:

             ClonableObjectContainer coc = new ClonableObjectContainer(actor);
             EntityObject editEntity = (EntityObject)coc.Data;
    


  • 执行更改后,从ObjectContext
    中分离原始对象,克隆对象,将其状态更改为 EntityState.Modified 并优雅地保存:

            ContextManager.CurrentObjectContext.Detach(oldItem);
            ContextManager.CurrentObjectContext.Attach((IEntityWithKey)item);
            ContextManager.CurrentObjectContext.ObjectStateManager.ChangeObjectState(item, EntityState.Modified); 
            ContextManager.Save();
    


  • 希望它有助于...
    Oran

    Hope it helps... Oran

    编辑:如果以下内容无效,请检查继续讨论:实体框架克隆后附加异常

    EDIT : If the followings does not work for you, please check the continued discussion : Entity Framework Attach Exception After Clone

    这篇关于WPF克隆/分离对象编辑问题 - 什么是标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆