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
25. September 2012 21:08
/
Rusty
/
Code
Comments (0)
I encountered a really frustrating bug yesterday when I tried to hit my MVC project on the QA server, and it turns out it manifested itself in several telltale ways:
- The QA server (IIS 7) reported 403 Forbidden for my login page. Could not load anything.
- My local machine (IIS 7 Express) would not show the ValidationSummary on the login page
- My local machine would not let me navigate to the forgot password page, or any [Allow Anonymous] pages other than the login page, unless I logged in;
- after logging into my local machine, and then everything worked fine, and I could get to the [Allow Anonymous] pages
- My local machine would not show the company name text in the banner until I logged in
The last sign was finally what clicked – I didn’t realize these all were related until I noticed that one. On my _layout.cshtml page, I have the following:
@{ Html.RenderAction("_CompanyName", "CompanyName"); }
The site is a multi-tenant application that has a url like: {company}.domain.com. The above calls into an action that takes that {company} and looks up the company name in the repository, then displays that name in the banner.
The problem was the _CompanyName action on the CompanyNameController was not marked as [Allow Anonymous]. IIS Express was able to let this slide, but IIS did not!
be0f234b-4ee2-4642-bcf3-8ff30b55824f|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
25. April 2012 16:27
/
Rusty
/
Code
Comments (0)
This isn’t quite a cheat sheet, but it is a quick reference to the workflow for using EF Database Migrations.
I’m also using MvcScaffolding on my controllers with the –Repository flag to stub out my controller, views, and repository.

b9aba195-7900-48bd-a68c-4b7306ab9bb8|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04