Use either:
public bool SaveRecording(Recording recording) {
( Edit: The query loads the CreatedOn column from the database and is therefore cheaper and faster than loading the full object. Since you only needed the CreatedOn property, using Find would be an extra overhead: you load all the properties, but you need only one of them. Except Facebook, loading a complete object using Find and then detaching it later can be a shortcut using AsNoTracking : db.Recordings.AsNoTracking().SingleOrDefault(r => r.Id == recording.Id); This loads the object without connecting it , so you don’t need to detach the object. Using AsNoTracking t Also speeds up the loading of the object.)
Edit 2
If you want to load more than one property from the database, you can create an anonymous type:
public bool SaveRecording(Recording recording) {
(End of editing 2)
Or:
public bool SaveRecording(Recording recording) { Recording oldVersion = db.Recordings.Find(recording.Id); recording.CreatedOn = oldVersion.CreatedOn;
( Edit: Using CurrentValues.SetValues contains only properties with a changed name that really have changed from the original state in the database. When you call SaveChanges , EF sends only the properties marked as changed in the UPDATE statement to the database. At that time how setting a state in Modified means that all properties are changed, regardless of whether they really have changed or not. The UPDATE statement will be more expensive since it contains an update for all columns.)
Slauma Nov 16 '11 at 4:00 p.m. 2011-11-16 16:00
source share