Saturday, 27 July 2013

Entity Framework Code First Seeding with Related Data

A quick tip I stumbled upon while working on an MVC application using Entity Framework Code First.

In order to Seed Related Data when using Entity Framework Code First, it’s important to give items temporary ID’s before calling the seed method. Otherwise, when the database is seeded you will end up with Conflicted Inserts

Wednesday, 26 June 2013

Knockout (or any javascript) Intellisense not working if move script file locations

I found while playing around with Knockout.js, that my Visual Studio Intellisense wasn’t working. This turned out to be because I had formatted my Scripts folder, and moved standard Javascript libraries into another folder.

This meant that Visual Studio couldn’t find my _references.js file.

At first I added the following to the @Scripts Section in my .vbhtml file;

   1: @Code 
   2:  
   3:        If (False) Then
   4:  
   5:            @<script src="/Scripts/lib/knockout-2.2.1.debug.js" type="text/javascript"></script>
   6:                
   7:        End If
   8:              
   9: End Code
Where the If (False) code forces the compiler to ignore the file when publishing.


But this would end up being silly if I had lots of files. So, I had a quick look around and found a couple of useful answers on Stack Overflow;


http://stackoverflow.com/a/12016530/1305169 and
http://stackoverflow.com/a/12628049/1305169


Where, basically, you need to add a reference to your new _references.js file to the Intellisense list options in Visual Studio;



  • In Visual Studio, go to Tools > Options > Text Editor > Javascript > Intellisense > References
  • In the “Add a reference to current group”, type in the relative path to your _references.js file (for me this was “~/Scripts/lib/_references.js”

Hey presto, you should now have working intellisense!

Wednesday, 19 June 2013

Slow WPF DataGrid when Grouping Styles Added

I found while working a project recently, that when I added the styling for Grouping, even if I wasn’t actually using it, that the load time for my data was massively affected.

Evidently, the reason or this slow down is due to the fact that, unbeknown to me, when grouping is enabled in WPF, then Virtualisation is disabled by default.

Virtualisation allows WPF to not physically render items which don’t appear directly in the UI, thus it doesn’t need to perform all the complicated UI math related to showing UI elements. This vastly improves performance, in my case, when dealing with Data Sources which have rows exceeding a few hundred.

Thankfully, WPF and .Net 4.5 has enabled a way for Virtualisation to be re-enabled when grouping. Simply adding the following to the Datagrid re-enabled Virtualisation.

   1: VirtualizingStackPanel.IsVirtualizing="True”