找不到类型或命名空间名称“DbContext" [英] The type or namespace name 'DbContext' could not be found

查看:52
本文介绍了找不到类型或命名空间名称“DbContext"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.NET MVC (3) 非常陌生,并且很难解决 Visual Studio 中的构建错误:

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

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.ComponentModel.DataAnnotations;使用 System.Data.Entity;命名空间 MyProjectName.Models{公共类 MachineModel{//名称[必需的][Display(Name = "Nom de la machine")]公共字符串名称 { 获取;放;}//IP[必需的][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")]公共字符串 IP { 获取;放;}}公共类 MachineDbContext : DbContext{公共 DbSet机器{得到;放;}}}

我得到的两个错误是:

  • 类型或命名空间名称找不到DbContext"(是您缺少 using 指令或程序集参考?)
  • 类型或命名空间名称DbSet"找不到(你是缺少 using 指令或汇编参考?)

我错过了什么?

解决方案

我遇到了同样的问题.事实证明,您需要 EntityFramework.dll 引用(而不是 System.Data.Entity).

我刚刚从 MvcMusicStore 应用程序中提取了它,您可以从以下位置下载:http://mvcmusicstore.codeplex.com/>

这也是如何在 MVC 中使用实体框架代码优先的有用示例.

I am VERY new to ASP.NET MVC (3) and am having a hard time resolving a build error in Visual Studio:

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

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?)

What am I missing?

解决方案

I had the same issue. Turns out, you need the EntityFramework.dll reference (and not System.Data.Entity).

I just pulled it from the MvcMusicStore application which you can download from: http://mvcmusicstore.codeplex.com/

It's also a useful example of how to use entity framework code-first with MVC.

这篇关于找不到类型或命名空间名称“DbContext"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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