Disable Entity Framework Code First Initialization Logic

When you use the Entity Framework Code First approach to build your database, EF’s default behavior is to verify if a database exists when the application runs the first time. In case it does not exist, EF creates it.

There might exist some situations when there is no need to run this initialization logic, e.g. when you run unit tests on an existing database.

Disabling the initialization for a given context type is really simple. This call will do the job:


I found a sample on Stack Overflow where a custom System.Data.Entity.IDatabaseInitializer<TContext> was implemented, having an empty InitializeDatabase method. But why create and maintain a class when we can get the same result for free.

Setting the initializer to null should be performed before the database is accessed the first time by the context type. I prefer to put this into the initialization method of my application or inside of the TestInitialize method of my unit test classes. Putting this code into the ctor of the DbContext class will lead to an unneeded number of calls every time an instance of the context class is created.

And because I forgot to do so, an additional hint concerning the unit tests: It is not sufficient to disable the initialization logic for the DbContext type defined / used in the test project. Also, the initializer of the DbContext type used by the application classes, defined in the application project, has to be set to null.


Understanding Database Initializers in Entity Framework Code First

Handle Race Conditions / Concurrency in Code First Entity Framework Applications


A common scenario in developing applications is the handling of concurrent database updates.

Given there is an application to manage customers, that is used by multiple users simultaneous.

User U1 reads the data of customer C1. While user U1 is looking at this data, user U2 reads the data of customer C1 too. Meanwhile, user U1’s phone is ringing. She picks it up. User U2 changes some of the customer’s data and saves it to the database. User U1 finishes the phone call, make some changes to the same customer, and saves it too.

Without handling the race condition or concurrency, al changes made by user U2 will be lost in this example. Handling it, the application should not save the changes made by user U1 and inform her that the customer’s data was changed in the meantime by another user.

In this post I will show an approach and sample code on how an application using the Entity Framework and the Code First approach can handle this.

Code Preparation

There is only one step required to make Entity Frame do all the work for you: Define a Version property in the model class (the name of the property does not matter at all), and decorate it with a Timestamp attribute.

/// <summary>
/// This column will be used by EF for race condition validation.
/// </summary>
public byte[] Version { get; set; }

Believe it or not – this is all you have to do (in case you are using the Code First approach).

Entity Framework will set the Version property to a value when the record is inserted into the database. Every time EF updates the record, it verifies that the value of the column has not changed. Doing an update, the version is increased.

Handling a Race Condition

EF throws a DbUpdateConcurrencyException in case a record was changed since the data was read.

So you have to catch this exception whenever your application updates or deletes database records and inform the user that the data was changed. In case there is no user to be informed, e.g. if this happens in a background process, you have to implement an appropriate error handling mechanism.

Using the Sample

To see EF Concurrency Handling in action, you can use the sample application.

There are two “clients” implemented, accessing both the same database record. Take these steps to provoke a concurrency exception:

  • Read the record for client 1 (press the Read Record button of the Client 1 group)
  • Read the record for client 2 (press the Read Record button of the Client 2 group)
  • Update the record for client 2 (press the Update Record button of the Client 2 group)
  • Try to update the record for client 1 (press the Update Record button of the Client 1 group)

The result should look like this:

Sample when Race Condition occurred

Some Sample Details

The sample code was created using Visual Studio 2013 Ultimate and Entity Framework 6.1. In case you use an older version of Visual Studio, you might have to create an empty solution and add the files to it.

The application expects a SQL Server instance (not SQL Server Express) installed on the local machine having the default server name (MSSQLSERVER). In case you do not have a SQL Server with this name running on your machine, you have to change the connection string in the app.config file before you can run the sample.

On the SQL Server, the application creates a database named EntityFrameworkRaceCondition. Please make sure to have the appropriate rights to create a database when you run the sample.


Microsoft Data Developer Center Entity Framework

Entity Framework / Get Started / Code First to a New Database

Entity Framework Tutorial Update Entity Using DbContext

Wikipedia Race Condition

Sample Application

Tracing in Microsoft Azure Cloud Service

Setting Up the Tracing

Since it took me some time to figure out again what to do to see the tracing output of my Microsoft Azure Worker Role in a Cloud Service, here are some hints.

First, read How To Enable Diagnostics in a Cloud Service. Following the step-by-step description solves most of the issues. I think it’s worth mentioning that there is no need to do any coding to enable tracing when following these steps. One just have to add the tracing messages themselves to the code.

Then, I created a new Cloud Service and a new Storage Account in the Microsoft Azure Management portal.

Before publishing the Cloud Services, I changed the size of the VM from the default value “Small” to “ExtraSmall” in the ServiceDefinition.csdef file. This size fits for testing purposes and is way cheaper.

After publishing the service to Windows Azure, I had to adjust the diagnostics settings manually. No idea why Azure was not using the settings of the config files – maybe I missed some. I used the Server Explorer of Visual Studio. Just right-click on the role node in the Windows Azure / Cloud Services tree, and make sure to take the proper deployment slot (production or staging). Then select Update Diagnostics Settings… and set the required values of the Application logs tab page.

Diagnostic Settings

Accessing the Output

To access the output, I use the Server Explorer of Visual Studio too. There is no need to install third-party tool for this.

The trace output can be found under the Storage node. Find the Storage Account created for the Cloud Service and open the Tables node. There should be a WADLogsTable item. If not, check all steps described by How To Enable Diagnostics in a Cloud Service and if you have updated the diagnostics settings. Double-clicking the WADLogsTable item will open the table containing the trace output.


How To Enable Diagnostics in a Cloud Service

Take Control of Logging and Tracing in Microsoft Azure (a little bit outdated, but still helpful)

Azure Execution Models (describes and compares Azure Virtual Machines, Web Sites and Cloud Services)

Enable / Disable Power Button on Start Screen of Windows 8.1 Update

After installing the Windows 8.1. Update on my Windows RT device, I was missing the power button on the start page (for new features in Windows 8.1. Update, see windows.microsoft.com/en-us/windows-8/whats-new).

Looking around in the Web, I learned that the appearance of the power button depends on the device type and size, not on the CPU architecture (x86/x64 or ARM) – see Windows Experience Blog.

Also, I found a hint about how to change the registry to enable / disable the power button myself.

But this post states that I should add a new value to the key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ImmersiveShell\Launcher, which means this change has only effect for the current user. Since I wanted to enable the power button for all users of the device, I added the following value to this key:


New value: Launcher_ShowPowerButtonOnStartScreen, type DWORD, value 1

Now all users of my Windows RT tablet have the power button on the start page.

Avoid CSS Footer Class To Overlap Content


Unfortunately I can’t recall where I found the initial implementation. Anyway, from somewhere I copied some CSS class definition to create a footer that is fixed at bottom of a web page, no matter how high the browser window and the page’s content is.

When using this in my ASP.NET MVC 5 Web application (that was based on Bootstrap, I noticed that, when I resized the browser window by making it narrower and less high, the footer was overlapping some of the page’s content when the page’s content needs to be scrolled.

My first thought was that something with Bootstrap went wrong. But later on I learned that Bootstrap was not involved here.

Because it took me some time to find a work-around, I will describe it here for later re-use.

The Footer Definition

The CSS Class

The CSS definition I found in the Web looks like this:

.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  /* Set the minimum height of the footer here */
  min-height: 60px;
  background-color: rgb(238, 238, 238);

The Footer Definition

The class was used like this by the _Layout.cshtml file:

<footer class="footer">
  <p>© @DateTime.Now.Year by proccelerate GmbH</p>


I found the explanation for the observed behavior on Stack Overflow:

“…[position: absolute]… takes your element out of the document’s regular flow, so there’s no way for the text to have any positional-relationship with it, unfortunately.”

I found a JavaScript based attempt to solve this here, but it did not worked correctly in my app.


Well, at least I added a “filler” below the page’s redenred body that makes sure there will be enough space for the footer in case the content of the page needs to be scrolled.
Maybe the height of the filler needs to be adjusted in your code. This depends on your page’s content.

This is the changed content of the _Layout.cshtml file (snipped):

<div class="container body-content">
  <div style="height:45px;"></div>



Stack Overflow: bootstrap footer hiding contents above it

Stack Overflow: Stop absolutely positioned div from overlapping text

Paging Made Easy with ASP.NET MVC 5, Entity Framework 6 and PageList.Mvc


In former days, implementing paging sometimes took a little bit of time. That was independent from the used platform, native Windows Client or Web or whatever.

Fortunately, that was then and this is now. Implementing paging in an ASP.NET MVC 5 Entity Framework 6 application really became simple. Just a few lines of code, and you’re done.

The Basics

The basics of paging in ASP.NET MVC are described by the Getting Started with EF 6 using MVC 5 tutorial Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application.

As the title says, you need EF 6 and MVC 5. The author Tom Dykstra suggests to use PagedList.Mvc. NuGet Must Haves lists this package on top of the Top 20 packages for paging. So I thought I’ll give it a try.

Using PagedList.Mvc

The usage is simple. As described by the ASP.NET tutorial, the package needs to be installed. I used the menu: Tools / NuGet Package Manager / Manage NuGet Packages for Solution….

Changing the View

The model of the view that should contain paging needs to be changed from IEnumetablle<MyModel> to PagedList.IPagedList<MyModel>. Also, the using of PagedList.Mvc and a link to the PagedList stylesheet needs to be added. After changing the code, the first tree lines of the view look like this:

@model PagedList.IPagedList<MyModel>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" 
  type="text/css" />

In case you want to show the current page and the total number of pages, add something like this to the appropriate place.

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
 of @Model.PageCount

The last change is adding the paging buttons into the view.

  page => Url.Action("Index", new { page }) )

Changing the Controller

Because the view expects a different kind of model, the controller has to generate it. And because we need to return the content of different pages, the requested page is passed as a parameter. Given that db.MyModels returns something of type MyModel, the controller might look like this:

public ActionResult Index
    int? page
  var items = for item in db.MyModels select item;

  return (View(items.ToPagedList(pageNumber: page ?? 1, 
    pageSize: 10)));

That's really simple, isn't it.

Paging Extended

The code above works really good. But I want to extend the paging a little bit.

Let the User Choose

The number of items shown per page is fix in the sample above. I do not like that. So I added a dropdown list to the view to let the user choose how many items per page will be displayed.

The list of items shown by the dropdown list is generated by a helper method to make it reuseable.

public static class DefaultValues
  public static SelectList ItemsPerPageList 
      { return (new SelectList(new List { 5, 10, 25, 50, 100 }, 
        selectedValue: 10)); 

To use this list, the view defines a local variable.

SelectList itemsPerPageList = DefaultValues.ItemsPerPageList;

And the dropdown list is placed wherever I like using the HtmlHelper

@Html.DropDownList("ItemsPerPage", itemsPerPageList, 
  new { @id = "ItemsPerPageList" })

To have the correct number of items per page shown when the user switches the page, we need to pass the current number of items to the controller. Therefor the HtmlHelper for the paging buttons needs to be extended.

@Html.PagedListPager(Model, page => Url.Action(ActionNames.Index,
  new { page, itemsPerPage = ViewBag.CurrentItemsPerPage }));

As you can see by this code, the controller needs some changes too.

public ActionResult Index
    int? itemsPerPage,
    int? page
  ViewBag.CurrentItemsPerPage = itemsPerPage;

  var items = for item in db.MyModels select item;

  return (View(items.ToPagedList(pageNumber: page ?? 1, 
    pageSize: itemsPerPage ?? 10)));

Handling an Empty List

From what I noticed, the Model of the view is null in case the list itself does not contain any items.

The extension method PagedListPager is not able to handle that. And of course, when showing the current page and the total number of pages, accessing a null reference leads to an exception too.

To avoid these exceptions, some more code is required.

@if (Model != null
  && Model.PageCount > 0)
      Page @(Model.PageCount < Model.PageNumber 
             ? 0 : Model.PageNumber) 
      of @Model.PageCount
@if (Model != null)
    @Html.DropDownList("ItemsPerPage", itemsPerPageList, 
      new { @id = "ItemsPerPageList" })

Minor Stylesheet Additions

In my ASP.NET MVC application, I have a footer defined in the _Layout.cshtml file. In some cases I saw that the paging buttons partly were overlapped by the footer. To avoid this, I added a bottom margin to the pagination container style and put this into the Content/Site.css file:

.pagination-container {


Even with these small additions, paging is made really easy. What I was not looking at is the performance of the database access. For web applications with small databases, this does not seem to be important from my point of view. In case you do have a large database and complex database requests where performance is an issue, I think it's worth to check out the impact of this implementation.


ASP.NET Tutorial: Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application

PagedList.Mvc on NuGet

NuGet Must Haves Top 20 NuGet packages for paging

Handle Deactivated JavaScript in ASP.NET MVC Web Application


Depending on what your ASP.NET MVC Web application is supposed to do, it might be necessary that the client’s browser has JavaScript support enabled.

If this is not the case, maybe you want to make sure the user will be informed and cannot use the application.

And because there is no JavaScript available, this has to be done in a non-script way.

Use The noscript-Tag & Redirect

To handle all this without any script is quite simple. HTML offers the noscript tag.

Inside the noscript area, one can add whatever HTML code is required.

Since I want the user to be redirected to another page, I will use meta http-equiv="refresh" content="0;…" to redirect the user to the page explaining that JavaScript is required to use my application.

Add It To The Layout Page

To make sure the user will be redirected independent which page should be opened, I add the noscsript area to the _Layout.cshtml page.

And because I do use a meta tag, the code is placed into the head area to avoid warnings that meta elements cannot be nested inside the body element.

In case you use different layout pages in your application, you should put the redirection code into a partial view and add this to all of your layout pages. If there is the need to change the redirection, there will be only one place that needs to be changed.

NoJScript Controller And View

I do have a ASP.NET MVC application, so want to redirect to an appropriate controller when JavaScript is disabled, not to a HTML page.

Accordingly, I add the NoJScript controller with an Index view to my project and redirect the user to /NoJScript. The view is used to inform the user that JavaScript support is required and can contain all non-script elements that are needed.

It is important that this view does not use the layout page itself. Otherwise there will be an endless redirection loop, because when the view is opened, the browser detects there is no JavaScript enabled, and again redirects to this view, and so forth.

Access Restrictions

In case you restrict access to your application by setting the AuthorizeAttribute either to your controller or by adding

filters.Add(new System.Web.Mvc.AuthorizeAttribute());

to the App_Start/FilterConfig.cs, remember to set the AllowAnonymousAttribute to the Index method of the NoJScript controller. Otherwise, the browser is hooked in an endless loop, switching between the login page and the JavaScript required page.

Putting It Together

Here are all changes listed.

In the _Layout.cshtml page, this section is added to the head element:

  <meta http-equiv="refresh" content="0;url=/NoJScript" />

The NoJScript controller is really simple:

public class NoJScriptController : Controller
  // GET: /NoJScript/
  public ActionResult Index()
    return View();

And the view should contain a little bit more information than this sample:

  ViewBag.Title = "JavaScript required";
  Layout = null; // <= Important!

<!DOCTYPE html>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>@ViewBag.Title - My MVC App</title>


 To use this application JavaScript is required.

Create a 1:1 Table Relationship using Entity Framework


My database roots are pure SQL. When I create a database, I use an Excel sheet for documentation purposes, which creates SQL statements to build up the tables.

Table relationships (parent / child) with foreign keys I do define by hand, like the good old craftsmen do. It’s simple for me, and having the appropriate SQL code from another project, it takes a few seconds to adapt it to the new tables.

But from time to time one should try something else, so I decided to use the Entity Framework (EF) Code First approach for a new project I started. This post was written to document the required steps for later re-use.

Starting Point

As a starting point, I built an ASP.NET MVC 5 application with EF 6 and “Individual User Accounts” authentication, using Visual Studio 2013 Update 1.

Project Settings

Because the key of the AspNetUsers table is a string containing a GUID, I wanted to add a UserProfile table to map the GUID to an integer. This integer value will be used by the application to map other data to specific users. From my point of view, an integer is much more readable than a GUID.

Adding the Model

Since I was using the Code First approach, I added a class called UserProfile to the models. To link the UserProfile to the user main table, it needs to contain a property of type ApplicationUser. This class is created by the ASP.NET MVC project template and maps to the database table dbo.AspNetUsers.

I also wanted to keep the date and time when a user was created, so the UserProfile class got a CreationDate property. And because it seems that EF Code First does not support database default values, this property is set by the default constructor of the class. As a result, UserProfile looks like this:

public class UserProfile
  public long Id { get; set; }

  public DateTime CreationDate { get; set; }

  public ApplicationUser User { get; set; }

  public UserProfile()
    CreationDate = DateTime.Now;

To make sure the UserProfile data will be read when the ApplicationUser is accessed, I added a UserProfile property to this class, which is located in the file Models/IdentityModels.cs.

public class ApplicationUser : IdentityUser
  public virtual UserProfile UserProfile { get; set; }

Try to Create the Database Tables

After creating the database itself using SQL Server Management Studio and changing the connection string in Web.Config, I used the Package Manager Console to create the database tables. The console can be opened via Tools / NuGet Package Manager / Package Manager Console (or use Quick Launch [Ctrl+Q]).

To enable database migration, I first ran Enable-Migrations, which failed.

The error message was “Unable to determine the principal end of an association between the types ‘OneToOneTableRelationship.Models.UserProfile’ and ‘OneToOneTableRelationship.Models.ApplicationUser’. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.“.

What does that mean? In short, EF does not know how the UserProfile and ApplicationUser relationship should be organized. Which one is the parent, and which one is the child. There are two ways on how to define this: by the relationship fluent API (means coding) or data annotations (means attributes on class properties).

Declare the Principal End of Association

I decided to use the coding approach and added the method OnModelCreating to the context class ApplicationDbContext (can be found in Models/IdentityModels.cs).

To complete the database context, I also added a DbSet<UserProfile> to gain access to that table.

public class ApplicationDbContext 
  : IdentityDbContext<ApplicationUser>
  public DbSet<UserProfile> UserProfile { get; set; }

  public ApplicationDbContext()
    : base("DefaultConnection")

  protected override void OnModelCreating
    DbModelBuilder modelBuilder
      .HasRequired<ApplicationUser>(profile => profile.User);


Create the Database Tables

Now that EF knows how to build up the relationship, I created the database tables with two steps at the Package Manager Console.

Add-Migration Initiallize

created the code for the initial table setup. This code is located at the Migration folder.


ran that code and created the database tables.

After the update, the database contains, beside others, these two tables:

AspNetUsers tableUserProfiles table

Setting Up the Controller

When Visual Studio creates the controller class from the ASP.NET MVC project template, the class AccountController does not provide a DbContext property. A DbContext is passed as a parameter directly to the newly created UserManager instance by the default constructor. Unfortunately, UserManager does not provide access to the encapsulated DbContext.

To be able to access to the DbContext, I changed the AccountController to look like this:

public class AccountController : Controller
  private ApplicationDbContext DbContext { get; set; }

  public AccountController()
    DbContext = new ApplicationDbContext();

      = new UserManager<ApplicationUser>(
          new UserStore<ApplicationUser>(DbContext));

// removed: public AccountController(UserManager userManager)

// the rest was left unchanged

Fill the Child Table

Now I was ready to add a new entry to the UserProfile table when a new user registers. The method AccountController.Register was changed like this:

public async Task Register(RegisterViewModel model)
  if (ModelState.IsValid)
    var user = new ApplicationUser() 
      UserName = model.UserName, 
      UserProfile = new UserProfile()

    using (TransactionScope scope 
      = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
      var result 
        = await UserManager.CreateAsync(user, model.Password);


      if (result.Succeeded)
        await SignInAsync(user, isPersistent: false);
        return RedirectToAction("Index", "Home");

  // If we got this far, something failed, redisplay form
  return View(model);

Creating the user is wrapped by a database transaction to make sure the user will be created completely or not at all. Because UserManager.CreateAsync is async, the TransactionScope needs to be created with TransactionScopeAsyncFlowOption.Enabled. Please notice that this option is only available for .NET Framework 4.5.1 or above. To use the class TransactionScope, a reference to System.Transaction needs to be added to the project.

The call to UserManager.CreateAsync is doing all the magic. It creates the records in the tables AspNetUsers and UserProfile. It also makes sure that reference between the tables is set properly, means it sets UserProfile.User_Id to the correct value.


I took the following steps to have my custom user profile data connected to the ASP.NET user table:

  • Add the child model class
  • Add a property of the new type to the existing parent class
  • Declare the principal end of association, i.e. add a OnModelCreating to the DbContext to define which class is parent, and which is child
  • Create the database tables using the Package Manager Console
  • Update the controller to be able to store and retrieve the additional data

Consuming the data is easy. Just access the corresponding property like this:

DateTime creationDate = user.UserProfile.CreationDate;


MSDN Data Developer Center: Code First to a New Database

Stack Overflow: What does principal end of an association means in 1:1 relationship in Entity framework

Stack Overflow: Get TransactionScope to work with async / await

Entity Framework: Loading Related Entities

Factory File Manager Updated to Ver.

The Factory File Manager was updated:

AppBar Seperator Style adjusted
#341: Copy path of focused item to clipboard and show the copied path in lower left corner
#348: Active index immediately changed when opening active tab selection (Ctrl+Tab)
#350: Close ButtonAppBar when “Same Path” was pressed
#352: Changed TextTrimming from WordEllipsis to CharacterEllipsis

Download the latest version from the Windows Store.