从数据库获取实体时如何不触发PropertyChanged?

时间:2017-06-16 06:33:05

标签: c# wpf entity-framework fody fody-propertychanged

我有WPF应用程序,我使用一些基本类。我还使用Fody PropertyChanged和EF 6.1.3。我试图在任何属性更改时更改属性SaveNeeded。所以我写这个课:

 public class Foo : INotifyPropertyChanged
{
    public Foo()
    {
        PropertyChanged += Foo_PropertyChanged;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }

    public int Id { get; set; }

    [NotMapped]
    public bool SaveNeeded { get; set; }

    private void Foo_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SaveNeeded = true;
    }
}

当我创建新的Foo然后更改一些属性时,它会像我想的那样工作。但是当我试图通过EF从数据库中获取Foo时,会触发PropertyChanged事件。有关如何从数据库获取Foo而不是触发PropertyChanged的任何建议吗?

谢谢!!

1 个答案:

答案 0 :(得分:0)

好的,谢谢我这样做的建议:

  public class Foo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }

    public int Id { get; set; }
}

 public class FooViewModel : Foo
{

    public bool SaveNeeded { get; set; }

    public void NoticeChanges()
    {
        PropertyChanged += FooViewModel_PropertyChanged;
    }

    private void FooViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SaveNeeded = true;
    }
}

所以我从数据库中获取Foo,使用Automapper映射属性,然后调用NoticeChanges