Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The Problem

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

Here are the referenced 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: #line hidden

All other pages work great - this only happens when accessing one page in particular. The reference is working on all other pages. As far as I am able to determine, this is not a problem with the reference.

I've spent a good block of time researching this issue.

All the answers I found suggested going to web.config and putting an assembly reference to linq in system.web > configuration > assemblies. Mine doesn't have assemblies listed and I suspect this is more for older versions. I did it anyway. It gave me another error saying it has no idea what to do with assemblies.

I deleted system.data.linq and added it in again.

I rebooted both VS and my computer.

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

Background - This is how this started:

The application is MVC 4, C#.

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

Here is some very similar code (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 fancy there.

The page is a typical razor view, strongly typed "List"...it has a table that it iterates to create, dumping the data as it goes.

I am happy to provide any additional code as needed.

I have not messed with web.config. I have not removed or tweaked any of the references on the pages. All of the other views work marvelously.

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

Thanks for your help!

THE SOLUTION: I went into references, and for the System.Linq (or for older versions, I suppose, System.Data.Linq) and changed CopyLocal to True.

like image 841
shubniggurath Avatar asked Mar 25 '13 17:03

shubniggurath


3 Answers

This worked for me:

  • Navigate to the web project's References node
  • Find the reference to System.Data.Linq
  • Open the VS Properties Window
  • In the properties window, change Copy Local: False to True

Before this, the System.Data.Linq.dll was not being copied into the bin directory. (Copying it manually into the bin directory also resolved the error)

like image 146
Chris F Carroll Avatar answered Oct 17 '22 21:10

Chris F Carroll


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

like image 28
Victor Petrykin Avatar answered Oct 17 '22 21:10

Victor Petrykin


MVC cannot compile your view on the fly. It looks like your view contains a reference to a DataContext. Adding a reference in web.config should tell the compiler to look 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 and datacontext code?

like image 29
Alexandr Mihalciuc Avatar answered Oct 17 '22 20:10

Alexandr Mihalciuc