2013-11-21 137 views
12

我有一個類似的問題,如問題 No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'提出的一個,該錯誤有以下消息:實體框架ADO.NET Sql.Data.Client提供商

「以不變的ADO.NET提供者名稱「System.Data.SqlClient」未在計算機或應用程序配置文件中註冊,或者無法加載。有關詳細信息,請參閱內部異常。

正如相關問題的答案所示,我通過軟件包管理器控制檯重新安裝了實體框架(EF6),但錯誤仍然存​​在。我也檢查了我的項目中引用了EntityFramework.SqlServer.dll。 這裏是存儲在App.config中的連接字符串:

<add name="DesignModel" ConnectionString="metadata=res://*/DesignModel.csdl|res://*/DesignModel.ssdl|res://*/DesignModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=C071E;initial catalog=CTD2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />  

我有另外一個項目中,我使用的EntityFramework創建完全相同的實體和上下文,並能正常工作,這使得這一切更令人費解。

試圖執行此線時,將顯示該錯誤:

DesignModel designContext = new DesignModel(); 
designContext.MoPerfIDs.Load(); 

其中DesignModel是繼承的DbContext的類的名字。

下面是完整的App.config:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="ppe.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
    <!-- 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> 

    <connectionStrings> 
    <add name="DesignModel" connectionString="metadata=res://*/DesignModel.csdl|res://*/DesignModel.ssdl|res://*/Design Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MONNMC071E;initial catalog=CTD2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 

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

任何幫助將不勝感激。提前致謝。

+0

,什麼是內部異常? – Pawel

+1

在bin中是EntityFramework.SqlServer.dll嗎? –

+3

我遇到同樣的問題。我不能簡單地創建另一個解決方案來解決這個問題我也不明白這是如何關閉的,因爲「無關緊要」。 – Bloodhound

回答

0

哇...這是非常特別的......反正。您需要爲System.Data.SqlClient SQL連接類型註冊實體框架提供程序。

你想下面的添加到您的app.config/web.config

<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="System.Data.SqlClient" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 
+0

這是否解決了這個問題 – RajeshKannan

+0

沒有RajeshKannan它沒有。 – Bloodhound

2

您需要註冊爲System.Data.SqlClient的SQL連接類型的實體框架提供。 你應該在的app.config:

<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> 
    <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> 
+0

OP沒有使用'LocalDb'檢查EF連接字符串。 – Aron

+0

Ack ...我犯了同樣的錯誤...我留下了一些CE代碼以及... – Aron

+0

謝謝你的迴應。我試着建議,但我仍然得到同樣的錯誤。我編輯了問題併發布了完整的App.config文件。非常容易混淆,因爲在另一個沒有出現錯誤的項目中,App.config中唯一的東西就是實體框架的連接字符串。 – MigLuev

0

仍不能確定是什麼原因導致的問題,但最終創建一個新的解決方案,並複製一切從我的項目。現在工作正常。真的很奇怪,的確如此。

0

有2個選項,你可以嘗試。

1)負載 「System.Data.SqlClient的」 手動

包括後續的聲明在上下文類

var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

2)不要裝入 「System.Data.SqlClient的」

通過改變從

<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> 

<providers> 
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
</providers> 
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
    <parameters> 
    <parameter value="v11.0" /> 
    </parameters> 
</defaultConnectionFactory> 

我希望你的問題得到解決。

+0

這些建議都沒有解決問題。 – Bloodhound

0

嘗試增加

VAR _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

在您的designContext的ctor中。

0

在我的情況下,問題是,爲了捕獲另一個異常,我啓用了CLR異常。我忘了禁用它。

我在我的例外設置中禁用了它。它忽略了這個異常,並繼續運行並自動爲我創建一個數據庫(在我的情況下)。

0

只需從nuget安裝「MySql.Data.Entity」!它會自動安裝mysql ado驅動程序和實體驅動程序!

0

我得到了這個錯誤,但對我來說這是完全不同的東西。

我不得不編輯:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config 

和:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config 

搜索DbProviderFactories都CONFIGS是這樣的:

<system.data> 
    <DbProviderFactories> 
     <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" /> 
    </DbProviderFactories> 
    <DbProviderFactories /> 
</system.data> 

當我刪除後<DbProviderFactories />一切開始工作再次。

我能夠通過看僅Unable to find the requested .Net Framework Data Provider並找到這個答案來解決這個問題:

https://stackoverflow.com/a/9929534/3850405