【问题标题】:Identifying when a property changed [duplicate]识别属性何时更改[重复]
【发布时间】:2018-10-01 14:51:21
【问题描述】:

是否可以知道实体本身内的属性何时被修改?

例如:

public class StudentEntity{
    public string studentId { get; set; }
    public string studentStatus { get; set; }

   public string getStatusChangeDate{
       get 
       {
        //if studentStatus change then return date
       }
    }
}

【问题讨论】:

  • 将其设为属性,然后在 setter 中添加您的代码
  • @RufusL 我忘了添加getter和setter
  • 如果你有一个setter,并且value不等于当前的属性值,那么你就知道它已经改变了。您是否在问如何创建属性更改事件?还是您只想拥有一个包含属性更改日期的第二个属性?如果属性发生变化,您具体想做什么?
  • 如果不先将其存储在某个地方,您将无法在编程中显示任何内容。你打算把它存放在哪里?

标签: c# entity-framework-4


【解决方案1】:

INotifyPropertyChanged 接口用于通知客户端(通常是绑定客户端)属性值已更改。

例如,考虑一个具有名为 FirstName 的属性的 Person 对象。为了提供通用的属性更改通知,Person 类型实现了 INotifyPropertyChanged 接口,并在 FirstName 更改时引发 PropertyChanged 事件。

要在绑定客户端和数据源之间的绑定中发生更改通知,您的绑定类型应该:

实现 INotifyPropertyChanged 接口(首选)。

为绑定类型的每个属性提供一个更改事件。

重写你的代码:

public class StudentEntity : INotifyPropertyChanged
{
    private string studentIdValue;
    public string StudentId
    {
        get { return this.studentIdValue; }
        set
        {
            if(value != this.studentIdValue)
            {
                this.studentIdValue = value;
                this.OnPropertyChanged(nameof(this.StudentId));
            }
        }
    }


    private string studentStatusValue;
    public string StudentStatus
    {
        get { return this.studentStatusValue; }
        set
        {
            if(value != this.studentStatusValue)
            {
                this.studentStatusValue= value;
                this.OnPropertyChanged(nameof(this.StudentStatus));
            }
        }
    }

    public string StatusChangeDate { get; set; }

    public StudentEntity(string studentId, string studentStatus)
    {
        // Don't invoke property-setters from the ctor to avoid raising the event prematurely), instead set the backing fields directly:
        this.studentIdValue     = studentId;
        this.studentStatusValue = studentStatus;
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}


class Program
{
    static void Main(string[] args)
    {
        var person = new StudentEntity("101", "Accept");
        person.PropertyChanged += Person_PropertyChanged;
        person.StudentStatus = "Reject";

        Console.ReadLine();
    }


    private static void Person_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        StudentEntity studentEntity = sender as StudentEntity;
        if (e.PropertyName == "StudentStatus")
        {
            studentEntity.getStatusChangeDate = DateTime.Now.ToString();
        }
    }
}

【讨论】:

  • .. 或者您可以在StudentStatus set 方法中设置日期。这会容易得多,并且 OP 不需要任何实际通知,只需显示值。
  • 会是....
  • @stuartd 如何在 StudentStatus 设置方法中设置日期?日期应仅在状态更改时更改。有没有办法在 StudentStatus 的实体设置器中执行此操作?
  • @user793468 添加了一个小答案来展示如何
  • 您的代码不正确:您不应在每次调用属性设置器时调用OnPropertyChanged - 这可能导致无限循环和重入问题。相反,仅当新值实际上不同时,才从 setter 调用 OnPropertyChanged。我现在将编辑您的答案,以帮助那些盲目地复制和粘贴 SO...
【解决方案2】:

您可以使用一种方法来设置该值。这意味着每次 EF 加载记录时,它都不会被覆盖:但当然你必须记住调用方法而不是直接设置属性。

public class StudentEntity {
    public string studentId { get; set; }
    public string studentStatus { get; set; }
    public DateTime studentStatusChanged { get; set; }

    public void SetStudentStatus(string status) {
        studentStatus = status; 
        studentStatusChanged = DateTime.Now;
    }
} 

示例:https://dotnetfiddle.net/kF8VZR

【讨论】:

  • 我将实施并验证,但这不会更新所有记录的状态日期吗?即使状态没有改变日期也会更新
  • 它确实会更新所有记录的状态日期,而不仅仅是更改状态的记录,我是否应该再次验证现有值?
  • 哦,对了,我明白了。每次EF加载记录时,都会调用set方法,覆盖日期。
  • 我正在努力将新值与实体本身的现有值进行比较;一旦实体被击中,似乎没有旧状态值的回忆,有什么建议吗?
  • 用一个方法来设置怎么样?不理想,但它会工作。编辑答案。
猜你喜欢
  • 1970-01-01
  • 2011-12-26
  • 2014-03-04
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2011-06-08
  • 2021-05-27
  • 1970-01-01
相关资源
最近更新 更多