2

史前史:在.NET項目中引用程序集。奇怪的行爲

我有一個簡單的ASP.NET MVC3應用程序。在項目文件中,我已經開啓了構建MVC的視圖:

<MvcBuildViews>True</MvcBuildViews> 

此外我使用實體框架4.0。我已經添加了對我的Web項目的System.Data.Entity程序集的參考。 (是的,我知道在UI中使用數據訪問層並不好,它僅用於測試)。 在項目文件中,它看起來:

<Reference Include="System.Data.Entity" /> 

然後我試着使用ObjectContext類在我看來(剃刀引擎),但我得到的錯誤信息:

errorCS0012: The type 'System.Data.Objects.ObjectContext' 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'. 

在我的另一項目文件我已經看到了System.Data.Entity的是其他人引用:

<Reference Include="System.Data.Entity" > 
    <Private>True</Private> 
</Reference> 

接下來,我已經改變項目文件中使用<Private>True</Private>子元素和問題(參考部分)已經解決了。

那麼,什麼是<Private>True</Private>以及它如何影響構建過程?

回答

2

Private = True將程序集複製到網站的「bin」文件夾中,並自動成爲網站引用的彙編詞典的一部分。

但您不想將框架程序集複製到bin文件夾中。只需添加以下到它駐留在「〜/視圖」文件夾中的web.config:

<system.web> 
    <compilation> 
     <!--add assembly references needed for MvcBuildViews build task--> 
     <assemblies> 
      <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     </assemblies> 
    </compilation> 

如果存在,它不需要指定的完整版本語法4.0.0.0.0那裏我會高興地被通知。

1

MvcBuildViews使用msbuild來編譯你的視圖。似乎Visual Studio和msbuild有一些區別。

<Private>True</Private>與從visual studio複製本地相同。

0

其實this solution工作對我來說

  • 在Solution Explorer中,右鍵單擊引用,然後從右鍵菜單中添加引用。

  • 將顯示「引用管理器」對話框。

  • 如果尚未選中System.Data.Entity,請從對話框的列表中選擇System.Data.Entity。確保已選中項目 旁邊的複選框。

  • 單擊確定。