Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架4.1为POCO对象实现INotifyPropertyChanged_Entity Framework_Ef Code First_Poco - Fatal编程技术网

Entity framework 实体框架4.1为POCO对象实现INotifyPropertyChanged

Entity framework 实体框架4.1为POCO对象实现INotifyPropertyChanged,entity-framework,ef-code-first,poco,Entity Framework,Ef Code First,Poco,我首先实现代码,MVC模式,并使用实体框架4.1。我把我的问题用粗体字写出来 让我们假设(为了简化)我有以下POCO对象(Department),并且我想知道在执行contextDB.SaveChanges()并更新它之后,它何时会更改,因此我实现了以下功能: public class Department : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; p

我首先实现代码,MVC模式,并使用实体框架4.1。我把我的问题用粗体字写出来

让我们假设(为了简化)我有以下POCO对象(Department),并且我想知道在执行contextDB.SaveChanges()并更新它之后,它何时会更改,因此我实现了以下功能:

public class Department : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, e);
        }
    }

    [Key(), Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    private string name;

    [Required]
    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            if (this.name== value)
            {
                return;
            }

            this.name= value;
            this.NotifyPropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    private string personInCharge;

    [Required]
    public string PersonInCharge
    {
        get
        {
            return this.personInCharge;
        }

        set
        {
            if (this.personInCharge== value)
            {
                return;
            }

            this.personInCharge= value;
            this.NotifyPropertyChanged(this, 
                    new PropertyChangedEventArgs("PersonInCharge"));
        }
    }
}
我有3个项目(类库),分别是M(模型)、V(视图)和C(控制器)

从视图(V)中,用户生成和事件,例如,通过按下按钮添加一个部门,以便引用控制器(C)的视图调用控制器中的方法“Add”

引用模型(M)的控制器(C)可以访问上下文,因为它实例化了从模型中的dbContext派生的类,并通过上下文更新实体“Department”,例如dbContext.departments.add(newDepartment)

当在模型中更新实体部门时,会引发上面在实体部门中描述的NotifyPropertyChanged,但我的问题从这里开始:如何对视图说,嘿!实体部门已更改,因此应更新查看部门

为了实现它,我实现了观察者模式,我的意思是视图部门有一个称为“更新”的方法,它被附加到由模型维护的观察者集合中,因此模型在属性更改时,会迭代该集合,并为每个视图和视图更新调用方法“更新”

我这里的问题是:我不知道如何从视图订阅上述类department中的event property changed,以便在department POCO对象(上面描述的类)中更改属性后,模型迭代包含附加的观察者的观察者集合,然后调用相应的“更新”方法,用于附着到集合的每个视图(观察者)。或者有没有其他更好的方法,而不是对POCO对象使用INotifyPropertyChanged

此外,我还看到了使用INotifyPropertyChanged的问题,我的意思是,例如,每次添加部门时,视图都会更新两次,因为NotifyPropertyChanged会引发两次,一次来自Name属性,另一次来自PersonInCharge属性。这里还有一个问题:如何只引发NotifyPropertyChanged事件一次而不是两次