Entity Framework, Technical

Running Entity Framework code first migrations programatically

Entity Framework code first migrations can easily be run programmatically. You can specify a specific migration, or you can just update to the latest migration.

To rollback all migrations (calls the “Down” method on each migration):

var configuration = new Configuration();
 var migrator = new DbMigrator(configuration);

To rollback or update to a specific migration:

var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
//Update / rollback to "MigrationName"

To update to the latest migration:

var configuration = new Configuration();
 var migrator = new DbMigrator(configuration);

//Update database to latest migration

Security, Technical

Making Microsoft Security Essentials behave like antivirus software

I have been running Microsoft’s free antivirus software, Security Essentials, on all of my home machines since it first was released.

On three separate occasions, I’ve discovered Trojans running on machines that are supposed to be protected by this antivirus software.

This freaked me out the first time, concerned me the second time and made me rage quit Security Essentials the third time. I’m now running Bit Defender at home.

I looked into these problems as I could not be the only one facing these issues. I was right – I found several forum posts by people complaining of the same problems.

Upon looking into the issue further, I discovered that there isn’t actually anything wrong with the detection on Security Essentials. In fact, it ranks quite nicely amongst alternative free antivirus solutions (Avast, AVG, Avira).

The problem appears to be its default settings. By default, Security Essentials will be setup to run at 2 am on Sunday, and will only look for an update on virus definitions just before it runs. If, like most home users, your PC may be off at 2 am on a Sunday, these two critical actions will not happen. No update. No scan. This will leave your PC with very little protection.

If you’re going to use Security Essentials, you need to tweak the settings to make it more protective of your PC. Below are my recommended settings. Fire up Security Essentials and navigate to “Settings”.

1. Scheduled Scan

I’d recommend having a daily “Quick Scan” at a time that you know your PC will be on. If you’re worried about the slowdown, simply limit your CPU usage. And remember, the slowdown and downtime that you get as the result of a virus will be a lot worse than any slowdown than you could get as a side effect of an antivirus scan:


2. Default Actions

If my antivirus thinks it’s found a severe or high alert, I want it removed:


3. Real-time protection

This should be on. If it isn’t, turn it on.

4. Excluded files and locations

Be sensible here. Add any directories and folders that you will be working on regularly that are unlikely to get infected. For example, as a developer, I know that my source code is unlikely to be effected by a virus. As I will be writing changes to these files to the drive regularly, I also do not want any slowdown as a side effect of the antivirus scanning my edited files:


5. Excluded file types

Again, you want to be sensible here and ideally have as few files as possible being scanned here. The default settings of .ini and .log files should be sufficient here.

6. Excluded processes

If you use any heavy applications for work, it is worth adding them into this list. As a developer, I tend to spend a lot of time in Visual Studio. I know this process is a safe one as I installed it and it came from a vendor that I trust:


7. Advanced

The only change I’d suggest here is setting Security Essentials to scan your removable drives:


Security Essentials should now be of a greater protective value to you. If you don’t think this will protect you enough, consider purchasing an Anti Virus solution.

Azure, Entity Framework, SQL Server

Drop all tables in a SQL Server database (Azure Friendly!)

Most online solutions to this problem will point you in the direction of the undocumented stored procedure, “sp_MSforeachtable“. This stored procedure takes in a string of SQL as a parameter, and will execute this string for each non system table in your database.

Here’s the problem: undocumented stored procedures suck. Their usage has been determined by reverse engineering. They are not officially supported. Their implementation and usage could change with updates and new versions, or could disappear totally.

Here’s something that illustrates my point: “sp_MSforeachtable” does not exist in SQL Azure. So, if your development environment is SQL Server 2008 but your production environment is SQL azure and you are using “sp_MSforeachtable“, you will get problems when you go live, which sucks.

Below is a simple, Azure and Entity Framework friendly bit of SQL that will drop all tables in your database, with the exception of your entity framework migration history table – “__MigrationHistory“:

while(exists(select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME != '__MigrationHistory'))
 declare @sql nvarchar(2000)
 + ']')
 WHERE TABLE_NAME != '__MigrationHistory'
exec (@sql)
 PRINT @sql


If you need to drop your table constraints first, the following code will allow you to do so:

 declare @sql nvarchar(2000)
 FROM information_schema.table_constraints
 exec (@sql)
 PRINT @sql

Credit to SQL Server Central for the above snippet.

MVC 3, MVC4, Razor, Technical

Using MVC4 bundling and minification in an MVC3 project

When you push any site to production, you should at least do some basic front end optimisation. Running through a number of optimisation steps will ultimately make your website load quicker and feel more responsive to the end user.

If this is the first you have heard about front end optimisation, I must recommend that you read “High Performance Websites” by Steve Souders.

One of the best ways to make your site load quicker is to make it smaller. Reduce the amount of data that needs to go from your server to the client machine. And one of the best ways to make massive gains in doing this is to Bundle and Minify the CSS and Javascript that your application uses.

MVC4 has made this incredibly easy with built in Bundling and Minification. However, you do not need to upgrade your entire MVC project to take advantage of this fantastic new set of features.

Firstly, open up the project that you want to optimise. I’ll be demonstrating this with a new empty MVC3 web application. Currently, the head of my _Layout.cshtml file looks like this:

 <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
 <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/modernizr-2.5.3.js")" type="text/javascript"></script>

So lets run the page and just see what’s going on under the hood:


So at the moment, my page is a massive 355.89 Kilobytes – and all it does is display some text – “Index”. That’s way too big – and we can see that the biggest and slowest loading areas of the site are the Javascript libraries that are being pulled onto every page of the site. (It’s worth noting at this point that you could used the already minified versions of the files. For the sake of this demo, I’ll be using the full fat versions).

So let’s get bundling and minifying.

Open the package manager console, and enter the following command to drag down the Microsoft ASP.NET Web Optimization Framework:

  •  Install-Package Microsoft.AspNet.Web.Optimization -Pre

Once added to your project, you can the begin configuring your bunding and minification. Open your Global.asax.cs file and add a reference to System.Web.Optimization

using System.Web.Optimization;

Then, go ahead and create a new static method, RegisterBundes:

public static void RegisterBundles(BundleCollection bundles)


Now, lets create some Bundles. I’m going to put all of my Javascript into one minified bundle, and my CSS into another minified bundle:

public static void RegisterBundles(BundleCollection bundles)
 var styles = new StyleBundle("~/Content/bundledcss").Include("~/Content/site.css");

 var js = new ScriptBundle("~/Scripts/bundledjs").Include("~/Scripts/jquery-1.7.1.js",

 BundleTable.EnableOptimizations = true;

There are three types of bundle that you can create:

  • Bundle – a non-minified bundle of a collection of files
  • StyleBundle – a minified bundle of a collection of css files
  • ScriptBundle – a minified bundle of a collection of Javascript files

Now, lets wire up a call to our new RegisterBundles method. In Global.asax.cs, locate your Application_Start method. Add the following line:


Now, when your application starts up, the bundles will be created. Now, all we need to do is to tell the views that they now need to load a bundled file and not the raw, unbundled and unminified css and js files.

In your _layout.cshtml file, or whichever file you have your stylesheets and javascript files referenced, swap out your raw file references. Firstly, add a reference at the top of your view to System.Web.Optimisation:

@using System.Web.Optimization

Now, lets swap the old references to our full fat files out:

 <link rel="stylesheet" type="text/css; href="@BundleTable.Bundles.ResolveBundleUrl("~/Content/bundledcss")" />
 <script src="@BundleTable.Bundles.ResolveBundleUrl("~/Scripts/bundledjs")">"</script>

And that’s us. Let’s test it out. Remember to build first:


And we’ve made a massive improvement. We have:

  • Reduced the number of requests (from 6 to 3, as we’ve bundled multiple files into one
  • Reduced the file sizes

This won’t take long to bring into an existing site and is definitely worth the benefits.

Edit: Of course, you should also move your Javascript to the foot of the html document for even more speed improvements.