3

I am trying to reference the System.Data.Entity.Validation (EF 4.1 version) namespace inside of a shared View in my MVC3 project. I've been able to reference other external libraries using:

@using Example.Namespace

I cannot, however, get the same thing to work when it comes to libraries that are part of the new 4.1 EntityFramework. I have tried adding the following to the web.config within the Views folder:

<add namespace="System.Data.Entity.Validation, EntityFramework, Version=4.1.10715.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

I think I am on the right track, because now the intellisense is blowing up for other external namespaces that used to work. Can someone help me figure out exactly what the web.config entry should look like for this?

EDIT: to be clear, I am trying to ultimately use DbEntityValidationException in my view, which, as far as I know, is part of the EntityFramework 4.1 DLL. I am following information in this post (http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page) which seems to suggest I need to add the namespace declaration to the section of the web.config file within Views (NOT THE PROJECT WEB.CONFIG).

I am still working through this and I have found that adding assemblies to the system.web/compilation/assemblies section of the View's web.config also 'works' in that it either breaks all intellisense or gives me part of the namespace I want. For example, I added:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

That allows me to type System.Data.Entity in my view, but nothing appears in Intellisense after that. If I change it to:

<add assembly="System.Data.Entity.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

After I close and re-open my project, the intellisense breaks on everything in my view and I see the following error: ASP.NET runtime error: Could not load file or assembly 'System.Data.Entity.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

DMC
  • 361
  • 4
  • 15

2 Answers2

3

Okay, figured it out through trial and error.

As it turns out, you must have the following entry in either your root web.config, or the View's web.config inside of system.web/compilation/assemblies:

<add assembly="EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
DMC
  • 361
  • 4
  • 15
0

Interesting, it works fine for me if I do:

@using System.Data.Entity

Are you sure you've referenced EntityFramework in your project References?

Could there be something else in your razor view that's causing the problem?

I don't have anything special in my Web.config, but I'll paste the sections just in case:

<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>

<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.Helpers, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

If you're still stuck, please post at least the first few lines of your view.

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • take a look... i edited my question. were you able to use System.Data.Entity.Validation members in your View once you added the @using System.Data.Entity? – DMC Sep 20 '11 at 22:15
  • I think you have the wrong namespace. It's actually `System.Data.Entity.Validation` that has `DbEntityValidationException` not `System.Data.Entity`. So, `@using System.Data.Entity.Validation` should do the trick. Once I put then in my test page, Intellisense lets me do `throw new DbEntityValidationException()`. – devuxer Sep 20 '11 at 22:25
  • BTW, if you are using Visual Studio, you can easily figure out what classes are in what namespace by right-clicking on the assembly in the References folder and choosing "View in Object Browser". – devuxer Sep 20 '11 at 22:27
  • Sorry, typo. I know that it is in System.Data.Entity.Validation - I can get this class to be picked up by intellisense everywhere else in my code EXCEPT for my Razor views. – DMC Sep 21 '11 at 12:53
  • Do you have "Microsoft ADO.NET Entity Framework 4.1 - Update 1" installed in your environment? VS 2010 with ASP.NET MVC3? If this is working for you, there must be something different about my environment. – DMC Sep 21 '11 at 12:57
  • By the way - I tried creating an empty MVC3 app and referencing with @using System.Data.Entity.etc, and it does not work. – DMC Sep 21 '11 at 17:29
  • @DMC, It definitely works for me--VS 2010, ASP.NET MVC 3, EF 4.1.10751.0. Could you paste a sample razor view. Perhaps there's an issue with your razor syntax? – devuxer Sep 21 '11 at 17:40
  • there is no issue with the razor syntax. i put the following in a empty mvc3 project's _ViewStart.cshtml file: @using System.Data.Entity @{ Layout = "~/Views/Shared/_Layout.cshtml"; } and it puts the red squigly line under Entity because it cannot find that within the 'System.Data' namespace I also had two other developers here try it and they get the same behavior in their environments – DMC Sep 21 '11 at 20:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3675/discussion-between-danm-and-dmc) – devuxer Sep 21 '11 at 22:02