无法添加控制器 [英] Unable to Add Controller

查看:259
本文介绍了无法添加控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC Web应用程序的开发。
我想我的加入模型和的DbContext类后添加一个控制器。

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'

我使用 EF-6.1.1 (最新更新)

以下是我的模型和上下文类..

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.

请提出一些解决这个问题。什么是它走错了..

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

在这里是通过它我加入控制器的过程

here is the process through which i am adding Controller

推荐答案

实体框架带来的 5个版本和6之间重大更改为了它去完全开源的,他们感动了所有的库带外,他们现在都完全内的EntityFramework大会的NuGet。这样做的副作用是很多的命名空间实体框架的改变:

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:

有关的DbContext和code第一类型的命名空间并没有改变。
  这意味着使用EF 4.1许多应用程序或更高版本,你不会
  需要改变任何东西。

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.

类型,如ObjectContext的那名previously在
  System.Data.Entity.dll已被转移到新的命名空间。意即
  您可能需要使用或导入指令来打造更新
  针对EF6。

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.

有关空间变化的一般规则是,任何类型
  System.Data这*移至System.Data.Entity.Core。*。换一种说法,
  只需插入Entity.Core。 System.Data这之后。例如:

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

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

您所看到的错误的原因是您正在使用MVC,这是针对实体框架的早期版本的previous版本。脚手架将被假定旧的命名空间。

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.

您可以尝试升级到MVC的最新版本和脚手架将再次合作。如果不是这样,降级EF6(我不建议这样做,它有很多真正伟大的功能)。第三个选项是每次手动修复您的脚手架code。

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.

这篇关于无法添加控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆