2014-01-17 64 views
33

我們在Code First中使用EntityFramework 6。我們有一個控制檯應用程序,它沒有引用EntityFramework,但從它的App.config中讀取連接字符串。它調用DatabaseInitializationUtilities程序集傳遞連接字符串作爲參數。未找到具有不變名稱「System.Data.SqlClient」的ADO.NET提供程序的實體框架提供程序。

DatabaseInitializationUtilities具有對EF6(EntityFramework和EntityFramework.SqlServer)的引用。它的App.config是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <configuration> 
    <configSections> 
     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IAuthentication" /> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost/SecurityServices/Authentication.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAuthentication" contract="SecurityService.IAuthentication" name="BasicHttpBinding_IAuthentication" /> 
     </client> 
     </system.serviceModel> 
     <entityFramework> 
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
      <parameters> 
       <parameter value="v11.0" /> 
      </parameters> 
     </defaultConnectionFactory> 
     <providers> 
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     </providers> 
     </entityFramework> 
    </configuration> 

當執行到行,其中DatabaseInitializationUtilities試圖運行一個腳本

context.Database.ExecuteSqlCommand(script.ScriptText) 

錯誤被拋出:

沒有實體框架提供發現具有不變名稱「System.Data.SqlClient」的ADO.NET提供程序。確保提供程序在應用程序配置文件的'entityFramework'部分中註冊。有關更多信息,請參見http://go.microsoft.com/fwlink/?LinkId=260882

我相信補救措施正是我在我的配置文件中,所以我不明白這個問題。

注意:ReSharper的是bluelining節點和報告 「元素‘的EntityFramework’具有無效子元素‘供應商’然而,部分是由的NuGet注射時,我安裝了EF6

任何想法

+0

看看以下問題:http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-be-loaded – Lloyd

+0

謝謝。對[http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded][1]的評論是有幫助的。 [1]:http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded – Dewey

+1

添加EntityFramework.SqlServer.dll到客戶端應用程序,並添加連接到其app.config文件的連接字符完成了任務。現在錯誤消失了http://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name –

回答

17

你有裝配EntityFramework.SqlServer.dll在你的應用程序的路徑? 我有同樣的問題,並複製到DLL應用程序的路徑後,每一件事情的罰款。

+0

我遇到了同樣的問題並通過添加EntityFramework.SqlServer.dll修復了這個錯誤 –

+1

的好主意。你節省了我的時間。 +1 –

+0

謝謝親愛的托馬斯:) – Ali7091

48

你需要創建一個參考,所以它將被複制到調試文件夾中稍後它可以在運行時訪問。

不要複製任何文件,只需創建此引用:

private volatile Type _dependency; 

public MyClass() 
{ 
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices); 
} 
+0

爲什麼這樣呢? – JwJosefy

+5

史詩般的勝利!它失敗的原因是隻有直接使用的組件纔會被複制。這個程序集沒有直接的引用,所以不會被複制。 – briantyler

+10

我認爲英孚團隊在這方面做了一些大的解釋。 – ProfK

13
+0

喲,這也是我的問題... 我刪除了整個數據庫,並從頭開始檢查解決方案,並在第一次構建後,我得到了這個確切的「錯誤按摩「:-) 插入揮發性參考,它的工作。 –

+0

那麼爲什麼不簡單提一下它的主要思想呢? – Deilan

+0

@Deilan的想法是一樣的在dh_cgn答案http://stackoverflow.com/a/21531672/52277 –

5

我有這個問題了。當我運行我的.net mvc應用程序時,一切都在本地工作,但是當我發佈它時,我得到了這個錯誤。問題是我不得不在我的web項目中引用EntityFrameworl.SqlServer,儘管我有單獨的Data項目。奇怪的是,這個錯誤只是在應用程序發佈時才拋出。這可能是微軟的一個嚴重錯誤。我用EF 6.1.1。

+0

我有這個完全相同的問題。我對ef程序集的引用是在我的mvc項目中引用的一個單獨的數據項目中。我從nuget安裝了ef到我的mvc項目,重新發布並運行。感謝您的修復 – Mike

2

對我來說,事實證明,我在web.config文件中的提供者引用是不正確的。我相信nuget包可能不包括提供者。

我代替我的供應商與這和它的工作:

<entityFramework> 
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
     <providers> 
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     </providers> 
    </entityFramework> 
4

這是因爲EntityFramework.SqlServer.dll不會被複制到您的項目。添加該DLL,它會希望工作。你可以從你添加datamodel的項目中找到它。

0

得到這種問題的最佳途徑是將參考EntityFramework.SqlServer.dll包含到您的項目中按F4(op屬性)將屬性更改爲Copy to Local = True,以便每一個發佈應用程序時,它將被複制到發佈的文件夾中。

0

我傾向於將EntityFramework添加到Web項目以及實際引用它的程序集中。

0

我也有過類似的問題

我的問題是通過以下操作解決: enter image description here

enter image description here

相關問題