Upgrading To Entity Framework 6

I recently updated an application from Entity Framework 5 to version 6. MSDN provides some instructions.
The upgrade required a couple of code changes for me.
1) I received a compilation error:
“The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.”

To resolve, I needed to add code to my class that inherited from DbContext:

public class MainContext : DbContext 
	{
		static MainContext()
        {
			Database.SetInitializer(null);
            // Include this line to register the SQL Provider with Entity Framework 6
            var dummy = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
        }

2) ‘EntityState’ was moved to a different namespace, so I needed to add a ‘using’:

using System.Data.Entity;

to accommodate the change:
System.Data.EntityState => System.Data.Entity.EntityState

One benefit in EF 6 was being able to access and log the SQL generated by an EF statement. I added code to my override of DbContext.Save.

var commandText = new StringWriter();
this.Database.Log = s => commandText.Write(s);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: