Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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
C# 标识属性何时更改_C#_Entity Framework 4 - Fatal编程技术网

C# 标识属性何时更改

C# 标识属性何时更改,c#,entity-framework-4,C#,Entity Framework 4,是否有可能知道在实体本身内何时修改属性 例如: INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。 例如>,考虑一个具有FieldNew属性的对象对象。为了提供通用属性更改通知,Person类型实现INotifyPropertyChanged接口,并在更改FirstName时引发PropertyChanged事件 要在绑定客户端和数据源之间的绑定中发生更改通知,绑定类型应: 实现INotifyPropertyChanged接口(首选) 为绑定类型的每

是否有可能知道在实体本身内何时修改属性

例如:


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

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

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

实现INotifyPropertyChanged接口(首选)

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

重写代码:


可以使用方法来设置该值。这意味着每次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;
    }
} 

示例:

将其设置为属性,然后将代码添加到setter@RufusL我忘了添加getter和setter如果您有一个setter,并且
值不等于当前属性值,那么您就知道它已经更改了。您是否询问如何创建属性更改事件?或者您只想拥有第二个包含属性更改日期的属性?如果属性发生更改,您具体想做什么?如果不先将其存储在某个位置,则无法在编程中显示任何内容。你打算把它存放在哪里?。。或者您可以在
StudentStatus
set方法中设置日期。这会容易得多,OP不需要任何实际通知,只需显示值即可。它将是…@stuartd我如何在StudentStatus set方法中设置日期?仅当状态更改时,日期才应更改。在StudentStatus的实体设置程序中是否有这样做的方法?@user793468添加了一个小答案,以说明我将如何实现这一点并进行验证,但这不会更新所有记录的状态日期吗?即使状态没有改变日期也会得到更新编辑会更新所有记录的状态日期,而不仅仅是改变状态的记录,我是否应该再次验证现有值?哦,对了,我明白了。每次EF加载记录时,都会调用
set
方法,该方法会覆盖日期;一旦实体被击中,似乎就不记得旧的状态值了,有什么建议吗?使用一种方法来设置它怎么样?不理想,但它会起作用。编辑答案。
public class StudentEntity : INotifyPropertyChanged
{
    private string studentId;
    public string StudentId
    {
        get { return studentId; }
        set
        {
            studentId = value;
            OnPropertyChanged("StudentId");
        }
    }


    private string studentStatus;
    public string StudentStatus
    {
        get { return studentStatus; }
        set
        {
            studentStatus = value;
            OnPropertyChanged("StudentStatus");
        }
    }

    public string getStatusChangeDate { get; set; }

    public StudentEntity(string studentId, string studentStatus)
    {
        StudentId = studentId;
        StudentStatus = studentStatus;
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        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();
        }
    }
}
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;
    }
}