Programing

유형 또는 네임 스페이스 이름 'DbContext'를 찾을 수 없습니다.

crosscheck 2020. 6. 19. 18:58
반응형

유형 또는 네임 스페이스 이름 'DbContext'를 찾을 수 없습니다.


ASP.NET MVC (3)을 처음 접했고 Visual Studio에서 빌드 오류를 해결하는 데 어려움을 겪고 있습니다.

유형 또는 네임 스페이스 이름 'DbContext'를 찾을 수 없습니다 (사용 지시문 또는 어셈블리 참조가 누락 되었습니까?)

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; }
    }
}

내가 얻는 두 가지 오류는 다음과 같습니다.

  • 유형 또는 네임 스페이스 이름 'DbContext'를 찾을 수 없습니다 (사용 지시문 또는 어셈블리 참조가 누락 되었습니까?)
  • 유형 또는 네임 스페이스 이름 'DbSet'을 찾을 수 없습니다 (사용 지시문 또는 어셈블리 참조가 누락 되었습니까?)

내가 무엇을 놓치고 있습니까?


나는 같은 문제가 있었다. System.Data.Entity가 아닌 EntityFramework.dll 참조가 필요합니다.

http://mvcmusicstore.codeplex.com/ 에서 다운로드 할 수있는 MvcMusicStore 응용 프로그램에서 방금 가져 왔습니다 .

또한 MVC에서 엔티티 프레임 워크 코드를 먼저 사용하는 방법에 대한 유용한 예입니다.


System.Data.Entity프로젝트에 대한 assebmly 를 참조 하거나 EntityFrameworkNuGet 패키지를 설치하면 모든 것이 자동으로 설정됩니다.


간단한 메모입니다. DBContext가 아닌 DbContext입니다. 즉 소문자 'B'. 전체 네임 스페이스 System.Data.Entity ...와 name을 입력하려고 시도 할 때까지 intelesense가 작동하지 않는 동안 동일한 문제가 발생했기 때문에 이것을 발견했습니다. 그리고 마침내 소문자 'b'옵션을 제안했습니다.

System.Data.Entity.DbContext


VS2010을 사용하는 것과 동일한 문제가있었습니다. 나는 이것이 실제로 대답이 아니라는 것을 안다. 나는 그것이 누군가에게 도움이 될 것이라고 생각했습니다. DBContext의 정규화 된 이름을 사용하여 문제를 해결했습니다.

대신에

public class MachineDbContext : DbContext

나는 사용했다

public class MachineDbContext : System.Data.Entity.DbContext

프로젝트를 재건했습니다. 갑자기 VS가 기뻤고 정규화 된 이름을 제거하고 DBContext를 사용할 수도있었습니다.


같은 문제가 발생했습니다 ... Package Manager Console에서 EF를 설치하면 나에게 도움이되었습니다.

명령은 다음과 같습니다 Install-Package EntityFramework


나는 같은 문제를 겪었다. 나는 VS2010을 표현했다.

(참고 :이 문제가 발생하면 EntityFramework.dll 참조를 확인하십시오. 누락되었을 수 있습니다.)

다음은 나를 위해 해결했습니다.

최신 MVC 3 도구 업데이트
를 설치 한 다음 EntityFramework 4.1을 설치
하거나 NUGet ie를 사용했습니다. Visual Studio 2010 Express 사용 (도구-> 라이브러리 패키지 관리자-> 라이브러리 패키지 추가 참조-> 온라인 선택-> EntityFramework)

이상하게도 작동하지 않았습니다 .. 그래서 수동으로 "EntityFramework.dll"에 대한 참조를 추가해야
했습니다. dll을 검색해보십시오.
"C : \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ "

이미 가지고 있다면 '.net'참조를 추가하십시오.

참고 : NuGet을 사용하면 솔루션 디렉토리 옆에 "패키지"폴더가 생성됩니다. 내부에 "EntityFramework.4.1.10331.0"폴더가 있습니다. "Libs"폴더에 "EntityFramework.dll"이 있습니다. 찾아보기 탭을 사용하여
참조를 추가 하고 위의 dll을 선택하십시오.

같은 스냅


Visual Studio 2010 express를 사용하고 C:\Program Files\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll있으며 문제 해결하기 위해 참조를 추가 하고 있습니다.


For step-by-step instructions, see this new MVC / EF tutorial series: http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application The tutorial assumes you have installed the latest MVC 3 Tools Update and provides a link in case you haven't.


If your compiler doesn't recognize

  • System.Data.Entity.Infrastructure
  • DbContext
  • DbSet
  • et alii,

make sure

  1. you have Entity Framework 4.1 installed on your machine;
  2. 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.


This helps really handy:

  1. Select the ProjectNAme project in Solution Explorer.
  2. From the Tools Menu, choose Library Package Manager which has a sub-menu.
  3. From the sub-menu choose Package Manager Console.
  4. At the console’s PM prompt type install-package EntityFramework then hit enter.

Visual Studio Express SP1 Right click in Solution Explorer > References > Add Library Package Reference > EntityFramework


Use CTP5 instead it is newer version with some changes in API. You have to add reference to EntityFramework.dll which is installed in CTP directory - default is: c:\Program Files\Microsoft ADO.NET Entity Framework Feature CTP5\Binaries\EntityFramework.dll for 32bit system. On 64bit system it will be placed in Program Files (x86).


This happened to me when I branched code. To fix it I right-clicked my project in Visual Studio, chose manage Nu-get packages, uninstalled EntityFramework, then re-installed it. Problem solved


I just had this issue and none of the other solutions worked for me. I'm using Visual Studio 2010 and I have two projects in my solution: UIProject and DataAccessProject. The UIProject has a reference to the DataAccessProject.

My UIProject was targeting the .Net Framework 4 but my DataAccessProject was targeting the .Net Framework 3.5 for some reason.

  • I changed both projects to target .Net Framework 4
  • Uninstalled then re-installed the Entity Framework NuGet Package.

Problems solved. Now I have references to both EntityFramework and System.Data.Entity


i had the same problem.I had to rewrite "DBContext" after adding reference to entityframework and it resolved the problem.
its means that Visual studio was not able to recognize the class automatically and waited for it to be re-typed. so its best to add reference first and then implement code. VS 2013 - ultimate used


I had to first uninstall EntityFramework(Uninstall-package EntityFramework (not case sensitive apparently)), then install it again(Install-package EntityFramework), via the PM Console


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.


Download http://www.dll-found.com/download/e/EntityFramework.dll

Paste it in (for x86)

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\

Then Right click on project -> add reference -> select EntityFramework

Bingo......


I had this problem, read the above answer and download the entityframework.ddl but found that it is alreadt referenced. So I added the namespace and problem was solved

using System.Data.Entity;

I am using Visual Studio 2010, SP1 installed


having referenced entityframework.dll both system.data.entity worked.


I had the same error but the problem was just an accidental problem with my model.

I accidentaly put...

public class MyModelDBContext : Context
{
 public DBSet<MyModel> MyModels { get; set; }
}

...inside of the model class.


As alternative way you can go HERE - instruction how to install any required dll.

Or you can download NuGet and manage it from VS


Right click your reference and go to manage NuGet packages, then choose online all, then NuGet package source in the search textbox type Entity Framework and install it.


1) Uninstalling Entity Framework from All projects

2) Restart Visual Studio

3) Reinstalling to all required projects

and it started working


There might be a case where you reference everything you need to and you can even Go to Definition by pressing F12 on DbContext class which takes you to System.Data.Entity namespace but you still get this nasty compiler warning. Make sure that the Target Framework of your project and that of the Entity Framework version you are using match. Go to Project->Properties->Application Tab. Change the Target Framework(can't exactly say to which one, trial and error will help.). Just my two cents.


I also get irritated from this issue many times and finally find the solution.Go to edmx file->Update Model from Database->Refresh->Finish. Clean Solution->Rebuild Solution


you can try on package manager console

PM> EntityFrameWork\enable-migrations


Like the others have suggested:

  1. Add the correct references and directives. But it still doesn't work? Maybe you have the same problem I did:

Have a look below and see if you can tell me what is wrong:

public class PanelLengthContext : DBContext { } ??!

Make sure the class name is not misspelt - (case sensitivity)!

  • DbContext is the correct spelling.
  • this is how it should look:
  • check the spelling. don't waste 20 min of your life like i did. public class PanelLengthContext : DbContext {}

HTH


I have the same issue as you, I'm unable to implement it in the Controller class while it works when I put it in the model class. Add these codes on the top of your controller class

 using TimeSheetManagementSystem.Data;
 using Microsoft.Extensions.Configuration;
 using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore;
 using Newtonsoft.Json;

I have faced same issue and then i have add the namespace

using Practiceusingentityframework.Models;

After Adding it's Working fine

Try this may help you to solve your problem

and before that install the entity framework from Nuget Manager

참고 URL : https://stackoverflow.com/questions/5741109/the-type-or-namespace-name-dbcontext-could-not-be-found

반응형