You can either use reflection, which several people have already mentioned, or create an interface. If you use automatically generated objects, they are defined using the partial keyword, so you can create another class file in the same project and give it the same namespace and class definition, and you can implement this interface. Then, in the code you indicated above, you check to see if the object implements your interface, if so, discards it, and then sets the values.
The advantage of the interface is that you do not use reflection (which can be an expensive operation), as well as any future objects that you create will work automatically, just implementing your interface.
In cases where your object properties do not match your interface, you can explicitly implement an interface that will handle any named violations.
Example: suppose you define an interface, IContainAuditProperties , and you have all your corresponding objects implementing this interface, you can do the following inside a block where you iterate over all your new / changed objects:
var entity = entry.Entity as IContainAuditProperties; if(entity != null) { entity.CreatedDate = DateTime.Now; entity.ModifiedDate = DateTime.Now;
Brian ball
source share