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

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

问题描述

我很新的ASP.NET MVC(3)和我有一个很难解决的Visual Studio生成错误:

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

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

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:


  • 类型或命名空间名称
    的DbContext'找不到(是
    你缺少using指令或程序
    集引用?)

  • 类型或命名空间名称'DbSet
    找不到(你
    缺少using指令或程序
    集引用?)

我是什么失踪?

推荐答案

我有同样的问题。原来,你需要的EntityFramework.dll引用(而不是System.Data.Entity的)。

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

我只是把它从MvcMusicStore应用程序,你可以从这里下载: HTTP://mvcmusicstore.$c$cplex.com/

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

这也是如何使用MVC code-首先使用实体​​框架的一个有用的例子。

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

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

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