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=220.127.116.11, 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.
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.
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:
The problem for me was that I was not able to find EntityFramework in the list, so I tried the next step.
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
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.
Modify web.config file
Then I opened web.config file and modified it by adding EntityFramework assembly into
<assemblies>tag and add following additional item in the list
<add assembly=”EntityFramework, Version=18.104.22.168, 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.