史前时期:
我有一个简单的 ASP.NET MVC3 应用程序。在项目文件中,我打开了构建 MVC 的 View :
<MvcBuildViews>True</MvcBuildViews>
我还使用 Entity Framework 4.0。我添加了对 System.Data.Entity
的引用组装到我的 Web 项目中。 (是的,我知道在 UI 中使用数据访问层不好,它仅用于测试)。
在项目文件中它看起来:
<Reference Include="System.Data.Entity" />
然后我尝试在我的 View (Razor 引擎)中使用 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>
它如何影响构建过程?
最佳答案
Private=True 将程序集复制到网站的“bin”文件夹中,在该文件夹中它们自动成为网站引用的程序集字典的一部分。
但是您不想将框架程序集复制到 bin 文件夹中。只需将以下内容添加到位于“~/Views”文件夹中的 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 的语法,我将很高兴获悉。
关于.net - 在 .NET 项目中引用程序集。奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11054123/