EF:检查对象是否已被修改

时间:2014-01-15 22:43:33

标签: c# wpf entity-framework mvvm

我有Listview个对象 当我从Listview中选择一个项目时,我需要知道selectedObject的{​​{1}}是否已被修改为是否激活Listview

这是我继续使用Mvvmlight和Entity Framework 5的方法:

saveButton

这是有效的,但速度很慢。
事实上,每当我从listview中选择一个对象时,我都要求DB知道public RelayCommand SaveObjectCommand { get; set; } public MainViewModel() { SaveObjectCommand = new RelayCommand(SaveObject, CanSaveObject); } private bool CanSaveObject() { using (DBContext ctx = new DBContext()) { //selectedObject is the object selected from the Listview Object dbObject = ctx.Object.Single(x => x.ID == selectedObject.ID); if (selectedObject == dbObject) //simplified return false; } } 是否等于DB中的同一个对象(我知道是否已经修改了selectedObject属性)

有更好的方法吗?
我听说过DBContext.ChangeTracker,但它似乎需要一个DB请求。

2 个答案:

答案 0 :(得分:0)

我无法添加评论。 你用的是什么数据库?它紧凑吗?

您的问题的解决方法:您可以声明属性

private List<Object> MyCollectionOfObjects {get; set;}

使用您自己的集合并在CanSaveObject()wethod中访问它。如果DB需要更新,您应该更新DB和此属性。

答案 1 :(得分:0)

您提到,您的textblocks可能textboxes可以修改所选对象属性,您可以在其中检查该对象CanSave。我建议查看IEditableObject界面

快速示例

public class Customer : IEditableObject 
{

    struct CustomerData 
    {
        internal string id ;
        internal string firstName ;
        internal string lastName ;
    }

    private CustomersList parent;
    private CustomerData custData; 
    private CustomerData backupData; 
    private bool inTxn = false;

    // Implements IEditableObject 
    void IEditableObject.BeginEdit() 
    {
        Console.WriteLine("Start BeginEdit");
        if (!inTxn) 
        {
            this.backupData = custData;
            inTxn = true;
            Console.WriteLine("BeginEdit - " + this.backupData.lastName);
        }
        Console.WriteLine("End BeginEdit");
    }

    void IEditableObject.CancelEdit() 
    {
        Console.WriteLine("Start CancelEdit");
        if (inTxn) 
        {
            this.custData = backupData;
            inTxn = false;
            Console.WriteLine("CancelEdit - " + this.custData.lastName);
        }
        Console.WriteLine("End CancelEdit");
    }

    void IEditableObject.EndEdit() 
    {
        Console.WriteLine("Start EndEdit" + this.custData.id + this.custData.lastName);
        if (inTxn) 
        {
            backupData = new CustomerData();
            inTxn = false;
            Console.WriteLine("Done EndEdit - " + this.custData.id + this.custData.lastName);
        }
        Console.WriteLine("End EndEdit");
    }

    public Customer(string ID) : base() 
    {
        this.custData = new CustomerData();
        this.custData.id = ID;
        this.custData.firstName = "";
        this.custData.lastName = "";
    }

    public string ID 
    {
        get 
        {
            return this.custData.id;
        }
    }

    public string FirstName 
    {
        get 
        {
            return this.custData.firstName;
        }
        set 
        {
            this.custData.firstName = value;
            this.OnCustomerChanged();
        }
    }

    public string LastName 
    {
        get 
        {
            return this.custData.lastName;
        }
        set 
        {
            this.custData.lastName = value;
            this.OnCustomerChanged();
        }
    }

    internal CustomersList Parent 
    {
        get 
        {
            return parent;
        }
        set 
        {
            parent = value ;
        }
    }

    private void OnCustomerChanged() 
    {
        if (!inTxn && Parent != null) 
        {
            Parent.CustomerChanged(this);
        }
    }

    public override string ToString() 
    {
        StringWriter sb = new StringWriter();
        sb.Write(this.FirstName);
        sb.Write(" ");
        sb.Write(this.LastName);
        return sb.ToString();
    }   
}
  

我请求DB知道selectedObject是否相同   DB中的对象(我知道selectedObject属性是否具有   已被修改)

在你的情况下,最好在内存中进行检查,而不是去数据库,看看是否有任何值与输入的值不同。

相关问题