asp.net-mvc 找不到类型或命名空间名称“DbContext”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5741109/
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
The type or namespace name 'DbContext' could not be found
提问by Chris
I am VERY new to ASP.NET MVC (3) and am having a hard time resolving a build error in Visual Studio:
我对 ASP.NET MVC (3) 非常陌生,并且很难解决 Visual Studio 中的构建错误:
The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)
找不到类型或命名空间名称“DbContext”(您是否缺少 using 指令或程序集引用?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace MyProjectName.Models
{
public class MachineModel
{
// name
[Required]
[Display(Name = "Nom de la machine")]
public string Name { get; set; }
// IP
[Required]
[RegularExpression(@"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
ErrorMessage = "Donnez une adresse IPv4 valide.")]
[Display(Name = "Adresse IP de la machine")]
public string IP { get; set; }
}
public class MachineDbContext : DbContext
{
public DbSet<MachineModel> Machines{ get; set; }
}
}
The two errors I am getting are:
我得到的两个错误是:
- The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)
- The type or namespace name 'DbSet' could not be found (are you missing a using directive or an assembly reference?)
- 找不到类型或命名空间名称“DbContext”(您是否缺少 using 指令或程序集引用?)
- 找不到类型或命名空间名称“DbSet”(您是否缺少 using 指令或程序集引用?)
What am I missing?
我错过了什么?
回答by Shaz
I had the same issue. Turns out, you need the EntityFramework.dll reference (and not System.Data.Entity).
我遇到过同样的问题。事实证明,您需要 EntityFramework.dll 引用(而不是 System.Data.Entity)。
I just pulled it from the MvcMusicStore application which you can download from: http://mvcmusicstore.codeplex.com/
我刚刚从 MvcMusicStore 应用程序中提取它,您可以从以下位置下载:http://mvcmusicstore.codeplex.com/
It's also a useful example of how to use entity framework code-first with MVC.
这也是如何在 MVC 中使用实体框架代码优先的有用示例。
回答by Darin Dimitrov
You need to reference the System.Data.Entity
assebmly to your project or install the EntityFramework
NuGet package which will setup everything for you.
您需要将System.Data.Entity
assebmly引用到您的项目或安装EntityFramework
NuGet 包,它将为您设置一切。
回答by John Thompson
Just a quick note. It is DbContext, not DBContext. i.e. with a lowercase 'B'. I discovered this because I had the same problem while intelesense was not working until I tried typing the full name space System.Data.Entity... and name and finally it suggested the lowercase 'b' option:-
只是一个快速的笔记。它是 DbContext,而不是 DBContext。即带有小写的“B”。我发现了这一点,因为我遇到了同样的问题,而 intelesense 在我尝试输入完整的命名空间 System.Data.Entity... 和 name 之前无法正常工作,最后它建议使用小写的 'b' 选项:-
System.Data.Entity.DbContext
System.Data.Entity.DbContext
回答by cab0
I had the same problem using VS2010. I know this isn't really an answer. I just thought it might help someone. I resolved it by using the fully qualified name for DBContext.
我在使用 VS2010 时遇到了同样的问题。我知道这不是真正的答案。我只是认为它可能会帮助某人。我通过使用 DBContext 的完全限定名称解决了它。
Instead of
代替
public class MachineDbContext : DbContext
I used
我用了
public class MachineDbContext : System.Data.Entity.DbContext
and rebuilt the project. Suddenly VS was happy, and I was even able to remove the fully qualified name, and just use DBContext.
并重建项目。突然 VS 开心了,我什至可以去掉全限定名,只用 DBContext。
回答by Felipe Skinner
I had the same issue... Installing the EF from Package Manager Console worked for me
我有同样的问题...从包管理器控制台安装 EF 对我有用
the command was: Install-Package EntityFramework
命令是: Install-Package EntityFramework
回答by ASHISH
I am using Visual Studio 2010 express and adding a reference to C:\Program Files\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll
solved the problem.
我正在使用 Visual Studio 2010 express 并添加一个参考来C:\Program Files\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll
解决这个问题。
回答by user669226
If your compiler doesn't recognize
如果您的编译器无法识别
- System.Data.Entity.Infrastructure
- DbContext
- DbSet
- et alii,
- 系统.数据.实体.基础设施
- 数据库上下文
- 数据库集
- 等,
make sure
确保
- you have Entity Framework 4.1 installed on your machine;
in your .csproj file, you have the following reference
<Reference Include="EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
within the element
<ItemGroup> ... </ItemGroup>
that houses references to other assemblies.
- 您的机器上安装了 Entity Framework 4.1;
在您的 .csproj 文件中,您有以下参考
<Reference Include="EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
在元素内
<ItemGroup> ... </ItemGroup>
包含对其他程序集的引用。
回答by Amitd
I had the same problem..I have VS2010 express..
我有同样的问题..我有VS2010 express..
(Note: If you see this problem try checking references to EntityFramework.dll .. May be it is missing.)
(注意:如果您看到此问题,请尝试检查对 EntityFramework.dll 的引用。可能是它丢失了。)
The following resolved it for me.
以下为我解决了它。
I installed latest MVC 3 Tools Update
then I installed EntityFramework 4.1
or using
NUGet ie. from with Visual Studio 2010 Express
(Tools->Library Package Manager -> Add library Package reference -> Select Online -> EntityFramework)
我安装了最新的MVC 3 工具更新,
然后我安装了EntityFramework 4.1
或使用 NUGet 即。来自 Visual Studio 2010 Express(工具->库包管理器->添加库包参考->选择在线->实体框架)
Strangely that didnt work..So i had to manually add a reference to "EntityFramework.dll"
try doing a search for the dll ..may be here
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\"
奇怪的是这不起作用..所以我不得不手动添加对“EntityFramework.dll”的引用
尝试搜索dll..可能在这里
“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework \v4.0\"
If you already have it..just add a '.net' reference.
如果您已经拥有它..只需添加一个“.net”引用。
Note: If you use NuGet ,it creates a folder "packages" along side your Solution directory.
You will find the "EntityFramework.4.1.10331.0" folder inside it.Within "Libs" folder you will find
"EntityFramework.dll" .
Add reference to itusing Browse tab and select the above dll.
注意:如果您使用 NuGet ,它会在您的解决方案目录旁边创建一个文件夹“packages”。您会在其中找到“EntityFramework.4.1.10331.0”文件夹。在“Libs”文件夹中,您会找到“EntityFramework.dll”。使用浏览选项卡
添加对它的引用并选择上面的 dll。
回答by BehranG BinA
This helps really handy:
这非常方便:
- Select the ProjectNAme project in Solution Explorer.
- From the Tools Menu, choose Library Package Manager which has a sub-menu.
- From the sub-menu choose Package Manager Console.
- At the console's PM prompt type install-package EntityFramework then hit enter.
- 在解决方案资源管理器中选择 ProjectNAME 项目。
- 从工具菜单中,选择具有子菜单的库包管理器。
- 从子菜单中选择包管理器控制台。
- 在控制台的 PM 提示符下键入 install-package EntityFramework,然后按 Enter。
回答by Shubham Tiwari
Your project unable to resolve EntityFramework classes until you not added it in your project. For adding EntityFramework support you have to follow this steps: Tools->Nuget Package Manager ->Manage Nuget package for solution browse EntityFramework It shows latest stable EntityFramework version. currently 6.1.3 is latest version Install it for the selected project.
您的项目无法解析 EntityFramework 类,除非您未将其添加到您的项目中。要添加 EntityFramework 支持,您必须按照以下步骤操作:工具-> Nuget 包管理器 -> 管理 Nuget 包以获取解决方案浏览 EntityFramework 它显示了最新的稳定 EntityFramework 版本。当前 6.1.3 是最新版本 为所选项目安装它。