‘System.Data.Linq.DataContext’ is defined in an assembly that is not referenced

August 7, 2009

I’ve just tripped over another odd LINQ2SQL problem *

Heres what happend:

  1. I added a LINQ2SQL class to my project
  2. Dragged a table to the surface designer, wrote some code
  3. Realised I had one of the column data lengths set incorrectly – I had it set to NVarChar(1) – Should have been more like NVarChar(20)
  4. Changed the underlying datatable definition in SQL Server
  5. Rather than refreshing the connection, deleting the tables class and re-adding, I amended the properties of the related field, changing the server data type to NVarChar(20) and the type
    (I think this is what caused the problem, although I cant be sure, and IU dont have time to test more right now!)
  6. I then started getting this error at runtime

    Compiler Error Message: 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=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.

    Oddly, the solution compiled without issue in Visual Studio

  7. I thought best to take the mighty compilers advice and do what it suggested…although upon checking the references in my project I saw that I already had a reference to System.Data.Linq – Version 3.5.0.0
  8. Tried a clean and rebuild – No go
  9. Checked the web.config – Voila, no assembly reference! I added this in manually:

    <comilation>
      <assemblies>
        <add assembly=System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 />

 

 This sorted the problem – Although I’d like to know what caused it int he first place!

* Ok, to be fair, this may be a Visual Studio thing, although I’ve never seen this with other assembles

Advertisements

27 Responses to “‘System.Data.Linq.DataContext’ is defined in an assembly that is not referenced”

  1. Benson said

    This was very useful. looks like one has to add reference of Linq to the solution .

  2. Anand said

    Thanks, I ran into this too, and this fixed it for me.

  3. Thank you, this fixed an issue for me.

  4. latif said

    Thank you for this, it saved my time.

  5. Ifta said

    Thanks a bunch, it save my time 😀

  6. JD said

    Just got a similar error after attempting to move a model project with a .dbml file in it from one solution to another. I have no web.config as my project is a library project vs a web project. Anyways, I simply had to just add System.Data.Linq to my References list via right click add Reference which is basically another way of achieving same solution as manually modifying .config file I suppose.

  7. mandar said

    awesum solved same proble.!!!

  8. Lili_Tagetes said

    Thanks! It solved the problem 🙂

  9. Amir said

    THANKKK YOUUUU !!!!!!

    YOU MADE MY DAAAAAAY !!!!

    I WAS TOTALLY STUCK !!!!!!

    THAT WAS VERY USEFUL !!!!

  10. Chris said

    Life saver! thank you.

  11. Digvjay said

    Thanks man..!!!!!!!
    it really help me alot..
    keep good work going. Very usefull.

  12. jerry said

    Thanks for the post. I tried the solution JD suggested and it works! Thank you both!

  13. alemat said

    the below error is displaying when i run my project after deployed

    Parser Error Message: The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    Source Error:

    Line 62:
    Line 63:
    Line 64:
    Line 65:

    do you have any solution?

  14. alemat said

    the below error is displaying when i run my project after deployed

    Parser Error Message: The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    Source Error:

    Line 62: <add assembl= System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

    Line 63: add assembly="System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35

    Line 64: add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089
    assemblie

    do you have any solution?

    • I’m not sure which assembly, but one of them has a version mis-match between you development environment and production – I suggest you check the versions of each assembly (In the GAC – Global Assembly Cache) of each one on your dev machine and production server(s).

  15. rich said

    NICEY! THANKS!!!!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: