When upgrading your Entity Framework to Entity Framework 6.1 (EF6) from version 5.0, you may meet a number of issues. I have summarized all the issues I’ve encountered and their resolutions for your reference.

Upgrade to EF6

Microsoft has provided one summary about upgrading to EF6. You can follow the steps to resolve most of the issues.

http://msdn.microsoft.com/en-us/data/upgradeEF6

The main change is all the code in core libraries (primarily System.Data.Entity.dll, shipped as part of .NET) has been moved to the out-of-band libraries (primarily EntityFramework.dll). The namespaces for many classes are changed and also some method were removed or replaced.  The above link provides the workouts for most of the issues. I will list all the other issues that may occur in your upgrade.

Could not find the conceptual model type

This issue will happen when there are mix up of code generated by different version of Entity Framework templates. The resolution is to upgrade those templates to the version 6.0 ones. The detailed steps were covered  in Step 3 in the above link (Upgrade to EF6).

However, when you install the Entity Framework 6.1, the template may not be installed together. Check this through New Item –> Data dialogue to find whether it is installed. You should be able to find the template ‘Entity 6.x EntityObject Generator’ as the following screenshot shows.

image

If it is not existing, search online and install it.

Issue: property Database was removed from class ObjectContext

Previously you may set connection string via Database property of your object context. However is not existing any more in EF 6.1. To fix it, directly pass into the connection string as parameter when initiate the object context class.

-- Previous code 5.x, 4.x
public DefaultBlogDataServiceProvider(string connectionString)
        {
           context = new BlogDbContext();
            if (!string.IsNullOrEmpty(connectionString))
            {
                context.Database.Connection.ConnectionString = connectionString;
            }
        }

-- New code 6.x

public DefaultBlogDataServiceProvider(string connectionString)
        {

            if (!string.IsNullOrEmpty(connectionString))
            {
                //context.Database.Connection.ConnectionString = connectionString;
                context = new BlogDbContext(connectionString);
            }
            else
                context = new BlogDbContext();
        }

Issue: ObjectSet<TEntity>.Add is replaced by ObjectSet<TEntity>.AddObject

You may get errors like the following:

Error    1    'System.Data.Entity.Core.Objects.ObjectSet<*>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Entity.Core.Objects.ObjectSet<*>' could be found (are you missing a using directive or an assembly reference?)   
Simply replace the Add method with AddObject

-- Previously 4.x, 5.x
public bool AddComment(BlogPostComment comment)
        {
            context.BlogPostComments.Add(comment);
            int i = context.SaveChanges();
            return i > 0;
        }

-- New 6.x
public bool AddComment(BlogPostComment comment)
        {
            context.BlogPostComments.AddObject(comment);
            int i = context.SaveChanges();
            return i > 0;
        }

Similar to this, ObjectSet<TEntity>.Remove is replaced by ObjectSet<TEntity>.DeleteObject.

Issue: EntityEntry class is removed

In the previous versions, we need to use EntityEntry class to update or edit object.

For example:

-- Previously 4.x, 5.x
public bool EditBlogPost(BlogPost post)
        {
            var blogOld = GetBlogPostById(post.BlogPostId);
            if (blogOld != null)
            {
                var entry = context.Entry(blogOld);
                entry.CurrentValues.SetValues(post);
                entry.State = EntityState.Modified;
                return context.SaveChanges() > 0;
            }

            return false;
        }

In EF6.x, we can directly use the EntitySet<TEntity>.ApplyCurrentValues to directly update the values, which is very easy to use.

The above example can be now replaced with only two lines of code.

-- New 6.x
public bool EditBlog(Blog blog)
        {
            context.Blogs.ApplyCurrentValues(blog);
            return context.SaveChanges() > 0;
        }

Summary

Entity Framework is growing quickly. Check out the following project site to keep your projects update to date. Have fun with EF6.x.

 http://entityframework.codeplex.com/

Entity Framework

info Last modified by Raymond at 8 months ago * This page is subject to Site terms.

More from Kontext

Entity Framework Core Code-First - Generate Covering Index with Columns Included

local_offer entity-framework local_offer asp.net core local_offer Azure local_offer C#

visibility 12
thumb_up 0
access_time 5 days ago

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code. Scenario For ...

open_in_new ASP.NET Core

local_offer sqlite local_offer .net core local_offer entity-framework

visibility 22475
thumb_up 2
access_time 3 years ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

open_in_new .NET Framework

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward