Resolve the Issues in Upgrading Entity Framework to Version 6.1

Raymond Raymond event 2015-01-13 visibility 6,783
more_vert

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

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts