I have, e.g. a ‘CreateDate’ property on all my entity classes, using EF 4.3 Code First. I would like to prevent any changes to this field ever being send to the database, except for when the object is first created.
Now I could make this property read only, but I am using an MVC4 front end, and this requires a new object to be instantiated and bound to incoming property values during HTTP POST operations. I could then read the original object and set values based on that, which is not too efficient, or store the value in a hidden field, which is not too secure.
I would preferably like to simply suppress any update to this field when my DbContext saves changes. Can I do this? How?
I would use a marker interface for the entities which have
CreateDatethen I would override theDbContext.SaveChangesand then with the help of theChangeTrackerset or reset theCreatedAtvalue.Maybe it sounds complicated but I’ve created a little sample where you can get started: