2012-02-26 25 views
25

我有一個「MVC 4」項目,我想將一個程序集添加到web.config文件中,但我不知道應該將它放在哪裏。我嘗試任何方式,但我無法找到解決方案。每一次我得到這個錯誤時如何在mvc的web.config文件中添加程序集4

You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

這是我的web.config文件:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=152368 
    --> 
<configuration> 
    <connectionStrings> 
    <add name="ParsGraphicEntities" connectionString="metadata=res://*/Entities.ParsGraphic.csdl|res://*/Entities.ParsGraphic.ssdl|res://*/Entities.ParsGraphic.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=ParsGraphic;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-UI.MVC.InternetApplication-2;Integrated Security=True" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="2.0.0.0" /> 
    <add key="webpages:Enabled" value="true" /> 
    <add key="PreserveLoginUrl" value="true" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/Login" timeout="2880" /> 
    </authentication> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers" /> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="System.Web.WebPages" /> 
     </namespaces> 
    </pages> 
    <profile defaultProvider="DefaultProfileProvider"> 
     <providers> 
     <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </profile> 
    <membership defaultProvider="DefaultMembershipProvider"> 
     <providers> 
     <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> 
     </providers> 
    </membership> 
    <roleManager defaultProvider="DefaultRoleProvider"> 
     <providers> 
     <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </roleManager> 
    <sessionState mode="InProc" customProvider="DefaultSessionProvider"> 
     <providers> 
     <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </sessionState> 
    </system.web> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

請幫助我。

感謝。

+3

爲什麼你認爲你必須在web.config中添加一個引用?它是否在設計時編譯?如果沒有,那麼我會建議使用添加引用對話框添加引用。或者更好,使用nuget和安裝實體框架 – rfcdejong 2012-02-26 19:12:04

+1

你檢查出http://stackoverflow.com/questions/5021248/could-not-load-file-or-assembly-system-data-entity – RickAndMSFT 2012-02-26 23:58:02

+0

@rfcdejong,我已經添加System.Data.Entity與添加引用對話框,但我必須添加此程序集在web.config – 2012-02-27 18:11:04

回答

40

這很簡單。看看你的統計部在你的web.config:

<compilation debug="true" targetFramework="4.0"/> 

現在打開的部分,把你的組件內是這樣的:在本節

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

圖書館在編譯時檢查,它沒有任何關係與運行時綁定有關。

+2

澄清:項目中可能存在多個Web.config。將上面的程序集信息添加到位於項目級別的配置中,而不是在您的Views文件夾中。不要忘記在第一個自動關閉編譯標記中刪除「/」結束標記 – 2012-12-10 21:18:02

+1

雖然這解決了我的問題,但我仍然不明白爲什麼我必須這樣做。我已經在我的項目中引用了System.Data.Entity,但是我得到了運行時錯誤,說它無法找到程序集。當我將它添加到程序集部分的web.config中時,問題就消失了。我需要有人解釋爲什麼我必須同時擁有這兩個。 – Ray 2015-02-24 21:32:10

+0

添加引用會添加到您的項目(項目文件和bin文件夾)中。但是,您不要部署您的項目文件。這意味着您可以將您的dll發佈到bin文件夾中,但是當您的web應用程序在第一次請求中編譯服務器端的所有應用程序時,它不知道它在那裏。正如我所說的那部分是在編譯時檢查的(因爲你的服務器可以重新編譯代碼)。這就是爲什麼你必須在web配置中包含這些信息,因爲web.config是在服務器上部署的文件。 – 2015-02-26 19:12:26

4

我剛剛在VS Express 2012 for Web上遇到過MVC 4的相同問題,但是,雖然Alfonso的答案比我在網上找到的其他任何東西都更有幫助,但遺憾的是沒有清除我項目中的錯誤。在對互聯網進行一些研究之後(無濟於事),然後只是圍繞Visual Studio進行研究,我發現了以下解決方案。

除了添加的EntityFramework到我的項目的引用和代碼加到我的web.config,我也不得不採取下列步驟來滿足編譯器:

  1. 右鍵單擊引用文件夾>添加參考...
  2. 展開窗口左側的Assemblies並選擇Framework。
  3. 滾動並選擇程序集列表中的System.Data.Entity。
  4. 確保已選中System.Data.Entity旁邊的框,然後單擊「確定」。
  5. 重建項目。
+0

幹得好。這是我失蹤的特別醬。 – Ellesedil 2015-01-26 18:54:55

相關問題