I have a field in my model class that’s coded as follows:
public string CreatedBy
{
get { return _CreatedBy; }
set { _CreatedBy = value; Created = DateTime.Now; }
}
public DateTime? Created { get; set; }
When the CreatedBy field is populated then it fills out the Created date automatically. The problem for me is that if I again set the CreatedBy field (yes it can happen) then the date gets updated with the current date again.
Is there a way that I can make it so the CreatedBy and the Created fields can be populated just once?
Use a backing field and check whether the value has been set already – if so, leave it unchanged: