I'm working with .NET C# and Entity Framework.
I have a situation where I need to update an object in the database but I'll keep one property untouched. I'd like to know is it possible to do that?
Take the following class for example:
public class Person
{
public int PersonId { get; set; }
public string Name{ get; set; }
public int Age { get; set; }
public DateTime CreationDate { get; set; }
}
So I would provide a HTML form in a page for the client to edit a person. Example:
<form>
<input type="hidden" id="PersonId" value="1" />
<input type="text" id="Name" value="Cesar" />
<input type="text" id="Age " value="28" />
</form>
Then I'd grab the values and send to the repository to update that Person. But it does not make sense updating CreationDate, and I don't want to provide that possibility.
So I could do it in 2 different ways.
1) I could put it in the form as a hidden field and post it back. (but as I said I don't want to provide that possibility)
2) In the repository get the Person, update it's fields and leave CreationDate date the same and then save it:
void UpdatePerson(Person p){
var person = db.Persons.Find(PersonId);
person.Name = p.Name;
person.Age = p.Age;
db.Persons.Attach(person);
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
}
I'd like to know a way that I could just pass "p" to the context and update it leaving CreationDate the way it is.
I hope I was clearer.
I'm sorry about the lack of information in the first post.
Thank you for your help.