C# 在 mvc 4 中找不到 DbContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16314715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:43:24  来源:igfitidea点击:

DbContext not found in mvc 4

c#asp.net-mvc-4.net-4.5

提问by

I'm creating a web application using Visual Studio 2012and Framework 4.5and I'm using MVC 4and I'm stuck with a problem.

我正在使用Visual Studio 2012and创建一个 Web 应用程序Framework 4.5,我正在使用MVC 4,但遇到了一个问题。

I need to use DbContextbut I'cant find that in my project means I added System.Data.Entity and it still gives an error

我需要使用DbContext但我在我的项目中找不到这意味着我添加了 System.Data.Entity 但它仍然给出错误

The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)

找不到类型或命名空间名称“DbContext”(您是否缺少 using 指令或程序集引用?)

How can I solve this...?

我该如何解决这个...?

采纳答案by von v.

The DbContextlives in EntityFramework.dll. See this for reference

DbContext居住在EntityFramework.dll请参阅此以供参考

You can get it from nuget by typing this command in the PMC

您可以通过在 PMC 中键入此命令从 nuget 获取它

Install-Package EntityFramework -Version 5.0.0

You can get into the PMC by going to Tools -> Library Package Manager -> Package Manager Consolein VS

您可以通过在 VS 中转到Tools -> Library Package Manager -> Package Manager Console来进入 PMC

回答by Sanjay Patel

DbContext is part of EntityFramework.dll and not System.Data.Entity.

DbContext 是 EntityFramework.dll 而不是 System.Data.Entity 的一部分。

It was introduced in EF 4.1. You can install appropriate NuGet package using visual studio 2012. e.g. Right click on Project and select "Manange NuGet Packages" and find appropriate EF version and install it.

它是在 EF 4.1 中引入的。您可以使用visual studio 2012 安装合适的NuGet 包。例如,右键单击项目并选择“Manange NuGet 包”并找到合适的EF 版本并安装它。

回答by Matt Hyde

In addition to the accepted answer, this may help people in a situation that has not yet been mentioned:

除了已接受的答案之外,这可能会对处于尚未提及的情况的人们有所帮助:

This problem occurred for me when I created a new mvc4 project in an existingSolution in Visual Studio 2012. The Solution already contained a working mvc4 project, but when I created the second project it could not find the System.Data.Entity package (including DbContext). The install-package command does not solve the problem in this situation, because the overall Solution already had the package installed:

当我在 Visual Studio 2012中的现有解决方案中创建一个新的 mvc4 项目时,我遇到了这个问题。该解决方案已经包含一个有效的 mvc4 项目,但是当我创建第二个项目时,它找不到 System.Data.Entity 包(包括数据库上下文)。install-package 命令在这种情况下并不能解决问题,因为整体解决方案已经安装了包:

PM> Install-Package EntityFramework
'EntityFramework 6.0.1' already installed.

The problem was that the EntityFramework package does not get automatically installed in new projects, you have to add it manually. Go to:

问题是 EntityFramework 包不会自动安装在新项目中,您必须手动添加它。去:

Tools -> Library Package Manager -> "Manage NuGet Packages for Solution..."

Search for EntityFramework in the box at the top right, click Manage, and then check the box for the new solution. The new project should now build correctly.

在右上角的框中搜索 EntityFramework,单击管理,然后选中新解决方案的框。新项目现在应该正确构建。

回答by Rajamohan Anguchamy

You can do that through the following steps :

您可以通过以下步骤做到这一点:

Right-click your Projectin the Solutions Explorer.

在解决方案资源管理器中右键单击您的项目

Choose Add Reference...

选择添加参考...

Look for the System.Data.Entity.dllfile (or the EntityFramework.dll) and add them to your Project.

查找System.Data.Entity.dll文件(或EntityFramework.dll)并将它们添加到您的项目中。

You can also accomplish the same thing through NuGet if you have it installed :

如果你安装了 NuGet,你也可以通过它完成同样的事情:

Right-click your Projectin the Solutions Explorer.

在解决方案资源管理器中右键单击您的项目

Choose Manage NuGet Packages...

选择管理 NuGet 包...

Search for the EntityFrameworkPackage.

搜索EntityFramework包。

Click Install to add it to your existing project.

单击安装将其添加到现有项目中。

After you have successfully added these to your application, you'll just need to add a using statement within the page that you are using your DbContext and DbSet objects on :

在您成功地将这些添加到您的应用程序后,您只需要在您正在使用 DbContext 和 DbSet 对象的页面中添加一个 using 语句:

using System.Data.Entity;

Reference Link

参考链接

回答by Lena

I also had this issue, and it didn't work until i've installed EntityFramework from menu -> Project -> Manage Nuget Packages...

我也有这个问题,直到我从菜单安装 EntityFramework -> Project -> Manage Nuget Packages...

回答by Zamito

This problem occurred for me when I created a new mvc4 project in an existing Solution in Visual Studio 2012. The Solution already contained a working mvc4project, but when I created the second project it could not find the System.Data.Entity package(including DbContext). The install-packagecommand does not solve the problem in this situation, because the overall Solution already had the package installed:

当我在Visual Studio 2012. 解决方案已经包含一个工作mvc4项目,但是当我创建第二个项目时,它找不到System.Data.Entity package(包括 DbContext)。该install-package命令在这种情况下并不能解决问题,因为整体解决方案已经安装了软件包:

PM> Install-Package EntityFramework
'EntityFramework 6.0.1' already installed.

The problem was that the EntityFramework package does not get automatically installed in new projects, you have to add it manually. Go to:

问题是 EntityFramework 包不会自动安装在新项目中,您必须手动添加它。去:

Tools -> Library Package Manager -> "Manage NuGet Packages for Solution..."

Search for EntityFramework in the box at the top right, click Manage, and then check the box for the new solution. The new project should now build correctly.

在右上角的框中搜索 EntityFramework,单击管理,然后选中新解决方案的框。新项目现在应该正确构建。