Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude column from being updateable in Entity Framework 4.1 Code First

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?

like image 909
Bikal Lem Avatar asked May 24 '11 18:05

Bikal Lem


1 Answers

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.

like image 129
Ladislav Mrnka Avatar answered Sep 22 '22 08:09

Ladislav Mrnka