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

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

问题描述

我是MVC Web应用程序开发的新手。
我在添加我的模型和DbContext类之后尝试添加一个控制器。



但是,当我尝试使用Entity框架的这个控制器,它给我一个错误的

 无法将类型为System.Data.Entity.Core.Objects.ObjectContext的对象转换为System.Data。我使用 EF-6.1.1   



< c $ c>(最新更新)



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

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

public string EmpId {get;组; }


public string EmployeeName {get;组; }
}


public class ModelContext:DbContext
{
public DbSet&EmpDetails>员工{get;组; }
}

当我尝试添加一个控制器时,我收到以下错误。 p>



请为此问题提出一些解决方案。这是什么问题..



这里是我添加控制器的过程$ /



解决方案

实体框架在版本5和版本6之间引起了破坏更改。为了让它走完全开放源代码,他们将所有的库移动到了带外,现在它们完全在NuGet的EntityFramework程序集中。这样做的一个副作用是实体框架的许多命名空间已经改变了:


DbContext和Code First类型的命名空间没有改变。
这意味着对于许多使用EF 4.1或更高版本的应用程序,您不会
需要更改任何内容。



类似于ObjectContext之前的
System.Data.Entity.dll已被移动到新的命名空间。这意味着
您可能需要更新您的使用或导入指令以构建
对EF6。



命名空间更改的一般规则是任何类型在
System.Data。*被移动到System.Data.Entity.Core。*。换句话说,
只是插入Entity.Core。之后System.Data。例如:



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,该版本是针对早期版本的Entity Framework。脚手架将要承担旧的命名空间。



您可以尝试升级到最新版本的MVC,您的脚手架将再次工作。或者是降级EF6(我不推荐这个,它有很多很好的功能)。第三个选项是每次手动修复脚手架代码。


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.

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

here is the process through which i am adding Controller

解决方案

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.

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

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