The type "System.Data.Linq.DataContext" is defined in an assembly that is not referenced

Problem

Error navigating to a specific page (in local debugging): CS0012: The type "System.Data.Linq.DataContext" is defined in an assembly that is not referenced. You must add a reference to the assembly "System.Data.Linq, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089".

The following are the lines of code in the error message:

Line 28:
Line 29:
Line 30: public class _Page_Views_blah_granny_cshtml: System.Web.Mvc.WebViewPage {
Line 31:
Line 32: hidden line

All other pages work fine - this only happens when accessing one page in particular. The link works on all other pages. As far as I can tell, this is not a link issue.

I spent a lot of time studying this problem.

All the answers that I found suggest switching to web.config and putting a link to the linq assembly in the system.web> configuration> assembly file. My mine has no assemblies, and I suspect this is more for older versions. I did it anyway. This gave me another mistake saying that she does not know what to do with assemblies.

I uninstalled system.data.linq and added it again.

I rebooted VS and my computer.

My code - as VS is generated by default - has System.Linq.

Background is how it started:

The app is MVC 4, C #.

I created a new class in my DataContext, added a new controller, and created a strongly typed view.

Here is a very similar code (most likely not needed, but just in case).

Two classes:

public class granny { public string data { get; set; } public string criteria { get; set; } } public List<granny> getGranny() { var a = from x in grannytable join dx in anothertable on x.criteria equals dx.criteria select new granny { data = x.somenewdata; }; return a.ToList(); } 

Here is the controller:

  public ActionResult granny() { return View(db.getGranny()); } 

Nothing special.

A page is a typical razor view, a strongly typed "List" ... it has a table that iterates over, dumping data as it appears.

I am happy to provide any additional code as needed.

I did not bother with web.config. I did not delete or correct any of the links on the pages. All other looks work wonderfully.

When I run it locally and try to go to / granny, I get the above error.

Thanks for your help!

SOLUTION I went to the links for System.Linq too (or for older versions, say System.Data.Linq) and changed CopyLocal to True.

+7
c # .net-assembly linq web-config asp.net-mvc-4
Mar 25 '13 at 17:49
source share
3 answers

This worked for me:

  • Go to the web project. Node links
  • Find the link to System.Data.Linq
  • Open the VS properties window.
  • In the properties window, change Copy local: False to True

Prior to this, the System.Data.Linq.dll file was not copied to the bin directory. (Copying manually to the bin directory also fixed the error)

+21
Dec 18 '15 at 13:34
source share

MVC cannot compile your view on the fly. It looks like your look contains a link to a DataContext. Adding a link to web.config should tell the compiler to search for the file:

 <configuration> <system.web> <compilation targetFramework="4.0"> <assemblies> <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </assemblies> </compilation> 

Note: the assembly should be added under the compilation element.

Can you share your view, controller code, and datacontext?

+8
Apr 03 '13 at
source share

Try adding @using System.Data.Linq at the top of your problematic view and <add namespace="System.Data.Linq" /> to the <pages> section of web.config in the Views folder.

+7
Apr 03
source share



All Articles