8

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.


  • Forgetting about assemblies in the config file, does your project not just have a "normal" set of references? (In solution explorer, is there a References item?) - Jon Skeet
  • There certainly is. It references System.Data.Linq. I removed the reference and added it, thinking something got hung up there. I'm using the included .NET System.Data.Linq. It points to C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Linq.dll as it should. It's worth noting that my other pages are using linq just fine. I don't think it's a linq reference issue, because it's not a gregarious problem - it's just this one page. - shubniggurath
  • can you provide a complete solution or project that demonstrates the problem? - Phil
  • The whole solution? Or are there specific files or properties you are interested in? - shubniggurath
  • @shubniggurath A complete solution. Doesn't have to be YOUR solution per say. Just one that demonstrates the problem. - Schandlich

3 답변


24

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)


  • +1 Nice one. This still holds up a year later. For anyone else, I experienced this with Umbraco 7 WebApi controller when using a dbml data context. Copy local as Chris describes does the trick. Cheers - Marc D.
  • Why does this work? Shouldn't System.Data.Linq.dll always be available anyway if it's referenced? - Protector one
  • It probably should. The bug, I suppose, is that it was marked Copy Local:False. I don't know how that happens - Chris F Carroll

8

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?



7

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.

Linked


Related

Latest