Does anyone know if we can exclude column from being updated in Entity Framework 4.1 Code First? For example I have 'CreatedOn' field that I don't want to included when doing edit/updates. Is this possible, i.e. selectively excluding field from update operation in EF Code First 4.1?
If you are working with attached entities you EF will generate updates only for fields which have changed. If you are working with detached entities you must manually say EF what has changed. If you call this:
context.Entry(yourEntity).State = EntityState.Modified;
you are saying EF that all properties should by modified. But if you instead call this:
context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true;
you will say that only SomeProperty
is modified (only this property will be in update). I'm not sure if you can do the reverse operation by marking the whole entity as modified and select properties which should not be modified but you can test it yourselves.
If your CreatedOn
is filled in the database you can mark it as DatabaseGeneratedOption.Identity
and it will be never modified by your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With