find a location for property in a new city

Monday, 19 July 2010

System.Data.Entity not referenced when using Entity Framework

I started getting a compilation error telling with a message telling me "CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'". This really confused me since the solution built fine but the error only occurred at run time.

Obviously, it is all about adding a reference to System.Data.Entity to my Web project right? I checked and it is already there. Removed it re-added it. Checked for the correct version but still it doesn't like it! Why?

This came about when I used an existing Models project from a different solution in a brand new MVC 2 web application. So I checked the differences between the working solution and the new solution. Both had the same reference in the Web project so what's wrong?

Solution

Finally, I found that I had to add the System.Data.Entity assembly in the web.config too! See line 6 below:
<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>

This miraculously made my page start working as expected again.

Follow britishdev on Twitter

33 comments:

  1. Thanks, man! Really helpful

    ReplyDelete
  2. Billy headache now gone. Billy happy. Billy love Colin.

    ReplyDelete
  3. Thanks a lot!
    It saved me a lot of time :)

    ReplyDelete
  4. Thanks very much

    ReplyDelete
  5. Even the updated MVC3 Tools (release a week or so ago) doesn't fix this - thank you!

    ReplyDelete
  6. Thx, very very helpfull. Saved me a lot of time

    ReplyDelete
  7. Thanks Very Very Helpful, Please give the reason for change

    ReplyDelete
  8. Because it makes it work :)

    I don't know man I wrote this ages ago!

    ReplyDelete
  9. YEAHHHHH !! I LOST 5 HOURS of time but You get STOP Frustation man, THANKS

    ReplyDelete
  10. Thanks man!!!!!

    ReplyDelete
  11. Thanks Man...it really worked..

    ReplyDelete
  12. Just saved me from a few hours of trouble shooting! Thanks much!

    ReplyDelete
  13. Thank you very much it saved my time.

    ReplyDelete
  14. Wow - thanks. This really stumped me.

    ReplyDelete
  15. Thanks it worked.....

    Sandesh Daddi

    ReplyDelete
  16. thanks, helpful

    ReplyDelete
  17. Thanks man!!!

    ReplyDelete
  18. Though in web.config I have the tag :



    Still I cannot access DbContext from System.Data.Entity namespace

    Any idea ?

    ReplyDelete
  19. Thanks you very much !!! I from Viet Nam :)

    ReplyDelete
  20. Method not found: 'System.Data.Entity.Core.Objects.ObjectResult`1 FYPDAL.FYPEntities.SP_GetProjectStudentsWithReg(System.Nullable`1)'
    the error occurs where a store procedure is called

    ReplyDelete
  21. I have updated edmx file in my EF project but all store procedures are not working
    System.MissingMethodException: Method not found: 'System.Data.Entity.Core.Objects.ObjectResult
    this error displayed when I run project

    ReplyDelete
  22. I like this blog! In return, I want to share some cheap ray ban sunglasses with you.

    ReplyDelete