2012-02-08 95 views
1

我想在ASP.Net MVC 3(剃刀视图)的视图页中使用实体框架对象,我很确定我以前做过这个。然而,无论出于何种原因,我不能让它在这个项目上工作:在ASP.Net中使用实体模型对象MVC视图页

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: 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'. 

Source Error: 

Line 1: @model Data.Post 

即在项目中引用(我可以操纵控制器中的数据),但我不能让它在工作风景。

回答

1

您是否尝试过加入:

<%@ Import namespace="System.Data.Objects.DataClasses.EntityObject" %> 
1

这是另一个原因,你不应该在页面上直接使用EF对象。如果添加一个您不想显示的属性并碰巧使用Html.EditorFor,会发生什么情况?该属性将被错误地写入页面进行编辑。

使用一个ViewModel,它只不过是一个具有要显示的属性的类。当你将你的EF对象向上的值复制到使用AutoMapper http://automapper.codeplex.com/

一个ViewModel类见我的意思在这里一个例子:

http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx

现在,如果你真的想坚持您目前使用的路线,然后通过一切手段为实体框架使用POCO模板,他们会自动将代码生成模板(.tt)安装到您的项目中,以便您的EF对象被视为“基本类(即POCO类)和他们将免除您正在经历的这种行李。你的View应该不知道你正在使用什么数据访问框架,并且不应该需要额外的参考才能使它工作。那么你显然会模糊图层。

1

我希望你已包括

System.Data.Entity的

到您的项目参考。

现在:

  1. 打开Web.config文件
  2. 转至组件部分: <compilation debug="true" targetFramework="4.0"> <assemblies> ..... </assemblies> </compilation>

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

希望工程!

相关问题