27. February 2013 19:24
/
Rusty
/
Code
Comments (0)
Just saw Julie Lerman show a slick way to set the modified date on an entity by overriding the SaveChanges() on a DbContext:

Other good tips:
- Download the EF PowerTools for a nice plug in that lets you rt-click on the class that implements DbContext and select EF\View Entity Data Model (read only) => to get a relational diagram of how EF is going to create your database before you do a migration.
- In your models, use both the navigation property and the foreign key to increase performance (w/out FK, EF has to go figure out what ID to put in there), to make some object graph management more intuitive for adding/updating or just figuring out if that navigation property exists without invoking lazy loading.
- For lookup-lists, you can improve performance by not tracking the entity state:
_context.MyEntity.AsNoTracking().OrderBy(a=>a.Name).ToList();
When you do the AsNoTracking, combine it with the previous tip so that EF doesn't add a new MyEntity when you save the parent each time:
parent.MyEntityId = Id; // instead of parent.MyEntity = myEntity <- that might cause EF to save a new (duplicate) myEntity to the database
5e6dc1fa-f193-42be-91ec-15c182deeb1c|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04