首页 文章

MVC,Views和System.Data.Entity

提问于
浏览
2

我有一个存储库项目,它被添加到我的MVC应用程序中 .

在我的MVC Web应用程序的C#代码中,我不得不添加对System.Data.Entity的引用,以便我可以从我的存储库访问对象 .

如果我没有添加引用,则以下操作失败;

DataRepository<Store> repo = new DataRepository<Store>();
List<Store> allSorted = repo.All(x => x.name);

现在我想将该列表传递给我的Partial View,它位于FVM中,它是我传递给Partial View的FVM .

所以index.aspx代码;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<StoresFVM>" %>

<% Html.RenderPartial("StoreList", Model.StoreListFVM); %>

和ASCX代码;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<StoreListFVM>" %>

<%        
    SelectList storeList = new SelectList(Model.Stores.Select(x => new { Value = x.number, Text = x.name }),"Value","Text");
%>

<%= Html.DropDownList("SelectedStore", storeList) %>

但是,我收到错误通知我需要在ascx中包含对System.Data.Entity的引用 . 我不明白为什么 .

我已经尝试将名称空间添加到web.config文件中,我尝试在ascx页面的顶部导入名称空间 .

思考?

EDIT

\ nasfile02 \ Visual Studio 2010 \ Projects \ TOM \ TOM \ Views \ Stores \ PartialViews \ StoreList.ascx(4):错误CS0012:类型'System.Data.Objects.DataClasses.EntityObject'在程序集中定义没有引用 . 您必须添加对程序集'System.Data.Entity,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用 .

EDIT

namespace TOM.FormViewModels
{
    public class StoresFVM
    {
        public StoreListFVM StoreListFVM { get; set; }
    }
}

namespace TOM.FormViewModels
{
    public class StoreListFVM
    {
        public List<Store> Stores { get; set; }
    }
}

1 回答

  • 17

    您应该确保web.config看起来如下所示:

    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    

    也描述于:https://stackoverflow.com/a/3611040/1737862

相关问题