.NET : How to solve System.Data.Entity.DbContext Compiler Error

Last updated: June 14, 2016

Recently I wanted to get a better understanding on what Entity Framework is all about. I looked for the blog posts dedicated to this topic and I found one such post with concrete example. I was eager to test the code, so I created a project in Visual Studio Express but to my surprise I was not able to successfully build the project as it gave me System.Data.Entity.DbContext Compilation Error.

It took me a while before I managed to get rid of this annoying error and I want to share the solution that worked for me so that it might also help others with the same issue.

The compilation error I was getting was this:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message:
CS0012: The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

The described solution in this post consists of 3 steps. You might not need to go through all them. I suggest that after each step try to build your project just to check if error still persists.

Note:Solution below was tested with this configuration:

  • Windows 7 Professional 64 bit
  • Visual Studio Express 2010 with Entity Framework 4.1 - Update 1 installed.

To solve the problem, I had to do the following:

Be aware:The solution below does not seem to work With Entity Framework 6.0 / Visual Studio 2015.
If you find the solution on the web, please share it in the comments. Thank you.
  1. add a reference to the EntityFramework.dll file in the project.

    First I tried the most probable cause of the compiler error, which is to add that missing reference of the EntityFramework assembly. To do that, you do the steps described below:

    • On the solution explorer, under your project, right-click references and click on Add Reference
      Add Reference Option in Visual Studio

      Alternatively you can go to Project menu and select Add reference.

    • Add Reference window will appear and under .NET tab, find EntityFramework. It should be located in the beginning of the list.
      .NET Tab when choosing Add Reference Option
    • Select the EntityFramework from the list and click OK. Now go to step 3.

    The problem for me was that I was not able to find EntityFramework in the list, so I tried the next step.

  2. Manually add EntityFramework from GAC to the references Assemblies for .NET

    Since I was unable to find the EntityFramework assembly in step 1, I decided to manually add it from GAC (Global Assembly Cache) and hoped this would solve the problem of adding the assembly reference to my project.

      • Locate the EntityFramework.dll from Global Assembly Cache folder and copy it to the Reference Assemblies folder.
      • Now try adding the reference as described in step 1.
    For locations of the folders in Win7 x64, check the table below:

    Global Assembly Cache Folder

    C:\Windows\Microsoft.NET\assembly\GAC_MSIL\EntityFramework\v4.0_4.1.0.0__b77a5c561934e089

    Reference Assemblies for .NET 4.0 folder

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

    After completed this step I was able to add EntityFramework reference to the project but the error still persisted.

  3. Modify web.config file

    Then I opened web.config file and modified it by adding EntityFramework assembly into <assemblies> tag.

  • Locate <assemblies>tag and add following additional item in the list
    <add assembly=”EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″/>

After doing all 3 steps, the project finally built successfully.

if you find this article useful, please drop a comment or consider sharing it on your favorite social network site.

Share this page

2 Comments

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top