Wednesday, July 20, 2011

Glimpse from NuGet

Some of you by already know the wonders of Glimpse for and the newer released Glimpse for MVC.  If you are not familar with this product I recommend that you check out the products website for more details.  (

I have recently been exposed to the wonders of Glimpse for MVC which the route detections, strongly typed entity inspections client side, and other various tools is hugely helpful in diagnosing what is happening within the views which you typcially do not have a lot of inspection options related to entities.

But I will give you a tip about this product and beware of a few issues.

I am a big fan of Lazy Loading for Entity Framework 4 really enjoy the simplistic nature of the syntax that allows for faster development with greater emphases on business logic versus lines of code.  So the biggest issue I have ran into is when children entities of children entities are lazy loaded and you have glimpse turned on for the site, a referenced entity has been disposed run time error will be generated.  Simply turn off Glimpse and issue will disappear. 

Given that Glimpse is currently in beta, I look forward to the polished product that will take these issues into account.  But for being in Beta, it is truely a great utility even with the few bugs that are in the product.

*** All Content is provided As Is ***

Using Extension Methods in MVC

As many .Net developers have learned extension methods provide a mechanism to quickly add on capabilities to existing data types and methods.  If you are unfamilar with Extension Methods, Microsoft provides a great tutorial and documentation via MSDN ( 

This particular post is related to utilizing these methods within MVC and the necessary work around for them to work with in your code.


  1. Create your extension method as normal, and put your Class in a folder you created to house your extensions (typically done in the App_Code folder in ASP.Net web apps - this is not a folder that is an available system folder for MVC)
  2. Perform a build via the build menu or CTRL+SHIFT+ B
  3. Attempt to add the Using statement to the top of controller, data model, or class.
Why is my namespace not showing up?

Well, this is because in MVC by default the class is added with a build action of "Content".  This means that when you build the application it did not compile your extension method class.

So how do you fix this?

This actually extremely easy via the following:
  1. Right click on the extensions class file you created
  2. Select Properties
  3. The first option in properties window is Build Action
  4. Select Compile from the drop down
  5. Build the application
Your extension methods namespace should now be available to reference in any of the other classes, controllers, data models via the Using statement. From then on within that particular class you can use your extension method as normal.

*** All Content is provided As Is ***

The Conversion of a datetime2 Data Type to a datetime Data Type Resulted In an Out of Range Value

This particular error was produced when using Entity Framework where the edmx is generated from a SQL Server 2008 database.  This particular issue was rather nerve racking giving that I was providing the property of the Entity that a DateTime.Now value.  Which works perfectly in other areas of the application, as expected.


There was two tables in question, TableOne has a non-nullable foreign key constraint to the primary key field of TableTwo.  This is highly common and not something that is an issue, obviously the record in TableTwo had already been created and the ID was being supplied to the TableOnes entity.  So when attempting to perform db.TableOnes.AddObject(tableone) and calling the db.SaveChanges() produced the following error "The Conversion of a datetime2 data type to a datetime datetype resulted in an out of range value." 


I verified through stepping through the code and inspecting the entity that the value being supplied for the only datetime property of the tableone entity was a valid datetime.  I attempted dropping the TableOne entity from the edmx and re-adding it.  Only to still produce the same error.  Given that the inner exception message was being provided from SQL Server, I decided to inspect the sql statement that entity framework was passing to the database since, the emdx has no datetime2 mappings for any of the tables to entities. 

Side Note:
To inspect the sql statements use SQL Profiler - a good tutorial and information is provided at

Upon executing the same action that produced the errors, I found that it was actually attempting to execute an insert statement for TableTwo instead of insert for TableOne.  Obviously, this means that there was an issue with the edmx file, and something is mapping correctly.

I inspected the mappings for both TableOne and TableTwo and did not see any incorrect mappings either in the designer view or via opening the edmx as an xml file.


As a last ditch effort since the deletion and re-addition of the TableOne to the edmx did not resolve the issue and the sql statement was trying to execute against TableTwo, I decided to try deleting and re-adding TableTwo.  This caused any mappings to get regenerated which solved the issue and everything saved properly with no conversion errors of datetime2 to datetime.

*** All Content is provided As Is ***

Monday, July 11, 2011

Mini Profiler for MVC

So a collegue of mine, sent me this little gem my way so I want to share it with everyone.

Given that slow downs in MVC can be hard to diagnose if its an issue with a route, database connection, or even a little JQuery script the team at Stack Overflow create this highly useful utility for all to use called MVC-Mini_Profiler. 

The Pros:
  1. Collects stats for MVC Routes, Database connections, and JQuery posts
  2. Has a strong group backing the utility in terms of future enhancements/compatibilities
  3. Simple and easy to understand statics
  4. Can be implemented at most levels of the application including even down to the individual lines of code.
The Cons:
  1. Can be a little tricky to get working
  2. Displays statics only on the page that you have setup profiling
  3. On or off situation configured in application_beginrequest and application_endrequest

For best implementations I can clearly see the benefit for using this as part of the UAT and performance monitoring. But not necessarly something that would I leave in the application for production unless only certain accounts are given this feature as it over shadows the underlying application.

For more information and detailed usage instructions check it out at

*** All Content is provided As Is ***

Thursday, July 7, 2011

An Item with the same key already exists

So you are probably thinking what I was thinking.  Thanks, that error is amazingly descriptive.

We all know that this is typically what you would expect to see this if you were using a dictionary and tried to add an already existing item with the same key.  But wait this is MVC3 and all I am trying to do is add a controller.  I have no control over the dictionary that MVCScaffolding (T4) MVC Tooling is using behind the scenes, what do I do now?

Well let me give you some tips on how to diagnose what the issue is and how it came about.  First let me say I have only ran into this when I used Entity Framework that is generated from a database.  The particular database was a newly created database with a couple hundred tables.  The build was at all times successful even though the root cause was related to foreign key constraints in the database.  But let me describe what steps I took to diagnose the issue and determine the resolution.

Background Info:
The controller and associated views I was trying to create were specific to entities located in my edmx file.  I had no business requirements for the particular entities that required a view model so I was building directly from the entity framework generated data context. The MVC3 application would build correctly with no warnings or errors, and I was able to update the entity framework model from the database with no warnings or errors as well.

So what exactly was the problem?
  1. One of the entities had a duplicate foreign key, which I confirmed was defined this way in the database as well.  
  2. Another entity seemed normal and there was no duplication of any key constraints.  But one of the foreign keys was created backwards, meaning that the primary key table was actually the foreign key table and vice versa.
  3. Another entity that caused the issue was due to the fact that the primary key in the one of the constraints was correct but the foreign key was actually pointed to the identity column of the foreign key table.
So how did I discover these were the actual issues?
  1. I removed the entities from the edmx with out deleting them out of the 
    1. This is done by right clicking on the entity
    2. Selecting delete
    3. But when the pop up comes up saying do you with to delete them choosing no.
  2. I then attempted a build knowing that it would throw errors.
  3. When I reviewed the errors I found the duplicate key entry issue, but for the other two issues I was not so lucky to find such an easy to fix mistake as the errors simply stated missing table reference.
After I found the issue I made the correction in the database and updated the model from the database.

Awesome, it will work now ... NOPE!

I wish it were that simple.  Instead, I had to remove the entities from the and then re-add them to the edmx.  For issues 2 and 3, I actually had to fully delete both the affected entity and the related entity that was part of the foreign key constraint from the edmx and add them again.  Fortunately this does not really affect any controllers, views or view models that you have already built unless you ended up renaming a field in the entities.  But you would run into this issue any time you rename a field in an entity.

I hope this help with the dreaded "An item with the same key has already been added" error from MVC3 when attempting to add a controller.

***** UPDATE Mon, Jul 11, 2011 at 9:48 AM *****

Steve Sanderson, Program Manager for .Net Stack at Microsoft has informed me that this issue is related to the MVC Tooling in VS 2010 and has been added to the bug list to be fixed.

*** All Content is provided As Is ***