1

I'm in the process of learning MVC 3, Razor, and EF Model First.

I have a project I'm working on where I have defined the EF Model in a separate project from the main web project. I'm trying to access data using that model in a view.

I get this error: enter image description here

I added the System.Data.Entity to my references.

Controller:

public ActionResult ListRole()
{
    AuthDbContainer db = new AuthDbContainer();
    List<Role> roles = db.Roles.ToList();

    return View(roles);
}

View:

@model IEnumerable<WebSecurity.Role>
@{
    ViewBag.Title = "Role List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
    <title>ListRole</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                desc
            </th>
            <th>
                createDate
            </th>
            <th>
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.desc)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.createDate)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.roleName }) |
                    @Html.ActionLink("Details", "Details", new { id = item.roleName }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.roleName })
                </td>
            </tr>
        }
    </table>
</body>
</html>

Thank you for any help.

UPDATE

This error happened because the assembly reference in my web.config was missing. It was added to my references in the project but not in the web.config. IIS pretty much tells me that in my error message. I should have read it better. Sorry for wasting anyones time. I added the following to my web.config and it works great now:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
m4tt1mus
  • 1,642
  • 14
  • 24

2 Answers2

3

Adding namespace to the Web.Config is not enough. When you add them to the Web.Config they serve as using directives in your views.

So you need Reference System.Data.Entity in your MVC project. (Right click References in your MVC project and click Add Reference and so forth ...)

Furthermore if you want to add using directives to your Razor views, you need to add it as follows

You need to configure section groups as follows

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

Then

  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <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" />
        <add namespace="System.Data.Entity" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Did you add a reference to EntityFramework in your MVC project?

Eranga
  • 32,181
  • 5
  • 97
  • 96
  • It is already added to the references, i will try your other suggestion though. Currently it isn system.web – m4tt1mus Jul 24 '11 at 03:29
  • If i try this i get another error: Unrecognized configuration section system.web.webPages.razor. But it autocompletes in the web.config so it must be it the right place. – m4tt1mus Jul 24 '11 at 03:38
  • @m4tt1mus edited answer. try adding `sectionGroup` to `Web.Config` and reference `EntityFramework` in your MVC project. – Eranga Jul 24 '11 at 03:46
  • uhm... System.Data.Entity is the entity framework im pretty sure. – m4tt1mus Jul 24 '11 at 03:57
  • @m4tt1mus look at [this answer](http://stackoverflow.com/questions/4136703/razor-htmlhelper-extensions-not-found/4136773#4136773) to configure namespaces globally in Razor – Eranga Jul 24 '11 at 04:28
3

This error happened because the assembly reference in my web.config was missing. It was added to my references in the project but not in the web.config. IIS pretty much tells me that in my error message. I should have read it better. Sorry for wasting anyones time. I added the following to my web.config and it works great now:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
m4tt1mus
  • 1,642
  • 14
  • 24