Exclude column from being updated in Entity Framework 4.1. Code first

Does anyone know if we can exclude columns from updating in Entity Framework 4.1 Code First? For example, I have a "CreatedOn" field that I do not want to include when editing / updating. Is this possible, that is, selectively excluding a field from an update operation in EF Code First 4.1?

+8
ef-code-first
source share
1 answer

If you work with attached objects, EF will only generate updates for fields that have been changed. If you work with individual objects, you must manually tell EF what has changed. If you call this:

context.Entry(yourEntity).State = EntityState.Modified; 

you tell EF that all properties must be changed. But if you call it:

 context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true; 

You will say that only SomeProperty changed (only this property will be updated). I'm not sure that you can do the reverse operation by marking the entire object as modified and selecting properties that should not be changed, but you can check it yourself.

If your CreatedOn populated in a database, you can mark it as DatabaseGeneratedOption.Identity and it will never be modified by your application.

+15
source share

All Articles