DbContext not found in mvc 4

I am building a web application using Visual Studio 2012 and Framework 4.5 and I am using MVC 4 and I am stuck in the problem.

I need to use DbContext , but I found that in my project it means that I added System.Data.Entity and it still gives an error

The type or namespace name 'DbContext' was not found (are you missing the using directive or assembly reference?)

How can I solve this problem ??

+6
source share
6 answers

DbContext lives on EntityFramework.dll . See this for reference.

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

 Install-Package EntityFramework -Version 5.0.0 

You can get into PMC by going to Tools → Library Package Manager → Package Manager Console in VS

+18
source

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

This problem arose for me when I created a new mvc4 project in an existing solution in Visual Studio 2012. The solution already contained a working mvc4 project, but when I created a 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, since the package is already installed in the general solution:

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

The problem was that the EntityFramework package is not automatically installed in new projects, you need to add it manually. Switch to:

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

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

+4
source

You can do this with the following steps:

Right-click the project in Solution Explorer.

Select Add Link ...

Locate the System.Data.Entity.dll file (or EntityFramework.dll ) and add them to your project.

You can also do the same thing with NuGet if you installed it:

Right-click the project in Solution Explorer.

Select Manage NuGet Packages ...

Find the EntityFramework package.

Click Install to add it to an existing project.

After you have successfully added them to your application, you just need to add the using statement on the page where you use the DbContext and DbSet objects to:

 using System.Data.Entity; 

Link Link

+3
source

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

+2
source

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

It was introduced in EF 4.1. You can install the appropriate NuGet package using visual studio 2012. For example. Right-click Project and select "Manange NuGet Packages" and find the appropriate version of EF and install it.

0
source

This problem arose for me when I created a new mvc4 project in an existing solution 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, since the package is already installed in the general solution:

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

The problem was that the EntityFramework package is not automatically installed in new projects, you need to add it manually. Switch to:

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

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

0
source

Source: https://habr.com/ru/post/944024/


All Articles