EntityFramework: Updating EF4.x to EF6

During the migration of an ASP.Net MVC application for a customer from an on premise environment to Microsoft Azure, I’ve run into some troubles updating EF so that we use the EF6 Retry Logic for Microsoft SQL Azure. This is a short how to overcome the problems I ran into. 1: Install the EntityFramework package First things first: if you didn’t do this already, add the EntityFramework NuGet package to your project. You can do so by using the tool (right click on solution > Manage NuGet packages for Solution) or if you need a specific version using the console app (Tools > NuGet Package Manager > Package Manager Console). 2: Check assembly redirects and namespace using’s A big difference between the 6.x and earlier version of EntityFramework are the required assembly redirects. This should be automatically fixed during the installation of the NuGet package, but make sure all references in your project(s) to System.Data.Entity are removed. If you used EF’s ObjectContext, make sure all the namespace using’s are still valid: all code regarding the ObjectContext has been moved to a new namespace (from System.Data to System.Data.Entity.Core). 3: Replace current code generation files The next step is to update all the code generation ( / items for your models and for that to work you first have to delete all existing files (tip: before you do this, make you sure you know if and which modifications have been made to the templates, you probably will want to make these changes again in your new templates). Add the EF6.x code generation files by right clicking on the design surface of your model and select Add Code Generation Item… from the context menu. In the following window select EF 6.x DBContext Generator. 4: Connection Resiliency Next thing to do is adding the actual Connection Resiliency. Just add a new Class to your project, preferably in the same project as your Model and call it something like MyConfiguration.cs. Add this content:

using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Data.Entity.SqlServer; 
namespace ModelProjectNamespace
    public class MyConfiguration : DbConfiguration 
        public MyConfiguration() 
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); 
            SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0")); 

What’s next Hit compile and you’re on your way. ObjectContext versus DBContext (and Microsoft SQL Azure) I’m well aware of the fact that there’s a EF 6.x EntityObject Generator template available for Visual Studio 2012 and 2013. If you didn’t install the template yet and do want to use the ObjectContext: while adding code generation files, instead of selecting the EF 6.x DBContext Generator, search online for the EF 6.x EntityObject Generator template. I didn’t find a way yet to make this work with Microsoft SQL Azure and Retry Logic. In short: you should consider the DBContext as an extra layer around the ObjectContext class. This extra layer provides extended API’s (such as DBConfiguration) that ObjectContext doesn’t have.

GDPR Consent

The ICT Group website uses cookies for various functions in the website; Functional Cookies, Website Statistics, Personalised Advertising and Social Media. Advertising and Social Media cookies collect information about activities of individual users. This allows third parties to display personalised adverts to you and to allow you the option to share content from this site to Social Media platforms.

By continuing to use this website you are allowing us to place these cookies. The Cookie Settings voor de ICT Group website can be changed or revoked at any time. You can reach the settings through the Cookie Settings button as well as through the link in the footer of every page on this site.

Read our privacy policy here | Close this bar
Cookie Settings