I’m using the entity framework and I have some properties which are common to all entities:
CreatedByUserName
CreatedDateTime
LastModifiedByUserName
LastModifiedDatetime
So currently I’m saving a new property and I’m having to go like this:
_db.ProjectApprovals.Add(projectApproval);
projectApproval.CreatedByUserName = "Dev";
projectApproval.CreatedDateTime = DateTime.Now;
_db.SaveChanges();
As you can imagine having to do:
projectApproval.CreatedByUserName = "Dev";
projectApproval.CreatedDateTime = DateTime.Now;
for all entities every save is a pain. I was thinking of wrapping the save and then I could do it in there. The problem is how would I know which entities in the context had been added or modified.
First of all you must define shared interface for all your entities implementing these properties:
Implement this interface in your entities and override
SaveChangesmethod in your derivedObjectContext.