Mahima Gandhe
Mahima Gandhe

Reputation: 651

Unable to Add Controller

I am new to MVC web application development. I am trying to add a controller after adding my model and DbContext class.

But when i am trying to this controller using Entity framework it gives me an error of

Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'

I am using EF-6.1.1 (latest update)

Following are my Model and Context Class..

 public class EmpDetails
    {
        [Key]
        public int Id { get; set; }

        public string EmpId { get; set; }


        public string EmployeeName { get; set; }
    }


 public class ModelContext : DbContext
    {
        public DbSet<EmpDetails> Employee { get; set; }
    }

When i am trying to add a controller I get following error.

enter image description here

Please suggest some solution to this problem. what is going wrong with it..

here is the process through which i am adding Controller

enter image description here

enter image description here

Upvotes: 3

Views: 2412

Answers (2)

Biren Mishra
Biren Mishra

Reputation: 1

While using ASP.Net MVC 3/4, Entity framework assembly (.dll) would be automatically referenced with a lower version (5.0.0.0). And when you update this to a higher version an explicit type conversion is required for which you are getting this error. one way to fix this problem is, use the existing version of Entity Framework (5.0.0.0) without updating to higher version.

Upvotes: 0

Dismissile
Dismissile

Reputation: 33071

Entity Framework brought breaking changes between versions 5 and 6. In order for it to go completely open source, they moved all of the libraries out of band and they are now all completely within the EntityFramework assembly in NuGet. A side effect of this was that many of the namespaces for Entity Framework has changed:

The namespaces for DbContext and Code First types have not changed. This means for many applications that use EF 4.1 or later you will not need to change anything.

Types like ObjectContext that were previously in System.Data.Entity.dll have been moved to new namespaces. This means you may need to update your using or Import directives to build against EF6.

The general rule for namespace changes is that any type in System.Data.* is moved to System.Data.Entity.Core.*. In other words, just insert Entity.Core. after System.Data. For example:

System.Data.EntityException => System.Data.Entity.Core.EntityException System.Data.Objects.ObjectContext => System.Data.Entity.Core.Objects.ObjectContext System.Data.Objects.DataClasses.RelationshipManager => System.Data.Entity.Core.Objects.DataClasses.RelationshipManager

The reason you are seeing the error is that you are using a previous version of MVC, which was targeting an earlier version of Entity Framework. The scaffolding is going to be assuming the old namespaces.

You can try upgrading to the newest version of MVC and your scaffolding will work again. Either that or downgrade EF6 (I don't recommend this, it has a lot of really great features). The third option is to manually fix your scaffolded code every time.

Upvotes: 1

Related Questions