This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

Resolve the Issues in Upgrading Entity Framework to Version 6.1

3772 views 0 comments last modified about 4 years ago Raymond

In this page

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

Related pages

Tuples in C# (4.x, 7.0, 7.1)

92 views   0 comments last modified about 4 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

141 views   0 comments last modified about 4 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Logging configuration in .NET core

273 views   0 comments last modified about 9 months ago

.NET core introduces a logging API that works with a number of logging frameworks. The built-in providers are configurable and extensible to support different level loggings.

View detail

Sending Emails in .NET Core Applications

1216 views   0 comments last modified about 9 months ago

Sending emails are common in applications. For example, when user registers, we need to send account activation emails. This post summarize the approaches we can use to send emails in .NET Core 1.x and 2.x.

View detail

Retrieve Http client request metadata like IP address and languages in asp.net core

834 views   0 comments last modified about 9 months ago

IP Address In ASP.NET Core, Request.UserHostAddress has been removed though that attribute exists in the traditional ASP.NET applications. We can use HttpContext.Connection to retrieve the remove client IP address: var ipAddress = HttpContext.Connecti...

View detail

Migrating from ASP.NET Core 1.x to ASP.NET Core 2.0

883 views   2 comments last modified about 10 months ago

Migrating from ASP.NET Core 1.x to 2.0 is not an easy job especially if you have customized Identity and used customized authentication. This post summarizes the issues and errors I have experienced and their resolutions when upgrading my project. Hopefully it can save you sometime if you are doi...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.