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

4163 views 0 comments last modified about 4 years ago Raymond

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

Set AttachDbFilename as Relative Path in .NET Core

203 views   0 comments last modified about 4 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

36 views   0 comments last modified about 4 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

SQLite in .NET Core with Entity Framework Core

118 views   0 comments last modified about 4 months 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 ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

80 views   0 comments last modified about 4 months ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

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

175 views   0 comments last modified about 8 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

368 views   0 comments last modified about 8 months ago

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

View detail

Add comment

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

Comments (0)

No comments yet.