2011-05-06 116 views
5

我正在創建我的第一個asp.net MVC網站(第3版)。ASP.Net MVC 3&System.Data.Entity?

我正在使用實體框架從我的數據庫中獲取數據,所以現在,我在我的數據庫中有一個電影列表。

我正在嘗試做一個顯示這些電影列表的頁面。

所以,控制器似乎沒問題,它返回一個View(IEnumerable)。

在視圖中,我指定我的模型的類型:

@model IEnumerable的

電影是與從我EDMX T4模板生成的類,所以它是從EntityObject heriting。

現在,當我嘗試顯示我的網頁,我得到一個錯誤,說明我說,我已經導入System.Data.Entity的:

Server Error in '/' Application.

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: Le type 'System.Data.Objects.DataClasses.EntityObject' est défini dans un assembly qui n'est pas référencé. Vous devez ajouter une référence à l'assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Source Error:

Line 27: Line 28: Line 29:
public class _Page_Views_Movie_List_cshtml : System.Web.Mvc.WebViewPage> { Line 30: Line 31: #line hidden

Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\93402ec0\8f8e48f4\App_Web_list.cshtml.9612c299.pwpwk-k5.0.cs Line: 29

但是,我引用這個dll在我的項目中,我有相應的使用在我的控制器中。

我試圖把這個用在CSHTML: @using System.Data.Entity的,但它不編譯(在System.Data找不到實體)

那麼,應該怎麼辦?

我所有的項目都是.NET 4中(而不是客戶端配置文件)

+0

哪個DLL你參考 - System.Data或System.Data.Entity? – 2011-05-06 17:21:09

+0

其中兩個 – J4N 2011-05-08 06:37:09

+0

爲了讓所有的編程愛好者都能卸載.NET Framework語言包以獲取英文異常消息。 – 2012-06-25 11:50:41

回答

1

兩件事類型枚舉 - 1.改變你的模型定義是由穆罕默德提到 - 其更標準的方式以及 2.包括參考: C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.Data.Entity.dll

或 在您的實體所在的任何項目中使用POCO Entity.Net模板,以將您從mVc項目中該庫的依賴關係中解放出來。

+0

我使用POCO實體發生器,它解決了我的問題 – J4N 2011-05-19 10:48:59

0

請嘗試指定如

@model IEnumerable<MyEntityObjectType> 
+0

我試過,但它不會改變任何東西 – J4N 2011-05-18 06:31:58

3

以下行添加到您的web.config

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

我想: <添加組​​件=「System.Data.Entity的,版本= 4.0.0.0,文化=中性公鑰= b77a5c561934e089" /> 但它不會改變任何東西 – J4N 2011-05-18 06:35:49

+0

添加此引用允許你指定在你看來@using System.Data.Entity的,它允許你引用實體 – 2012-09-06 13:17:02

1

對於Asp.Net MVC 2和3

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

對於ASP.NET MVC 4(.NET4.5)從

變化
<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 

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