Recently I wanted to get a better understanding of what Entity Framework is all about. I looked for the blog posts dedicated to this topic and I found one such post with a 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:
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.
- 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:
-
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
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.
- 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.
- On the solution explorer, under your project, right-click references and click on Add Reference
-
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.
- Locate the
-
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
After doing all 3 steps, the project was finally built successfully.
if you find this article useful, please drop a comment or consider sharing it on your favorite social network site.
ahammed
June 14, 2016i didn't see my entity file in my gac_msil.
admin
June 14, 2016Thank you for notifying me. It seems the solution is not relevant anymore with newer versions of Entity Framework and Visual Studio. I have added a notice in the post. If you find a solution, please share it here and I'll add it in the post. Thank you.