2014-01-08 44 views
5

作爲一個EF noob,我試圖使用實體框架6 Code First與MySql Server 5.6,我安裝在我的開發計算機上。MySql連接器EF6

我做了一個非常小的測試控制檯項目。我已經添加了的NuGet包:

  1. 的EntityFramework 6.0.2
  2. MySql.Data
  3. MySql.Data.Entities.EF6

我的App.config看起來是這樣的:

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

A有兩類:

MyContext:

public class MyContext : DbContext 
{ 
    public MyContext(DbConnection connection) 
     : base(connection, true) 
    { 
    } 

    public DbSet<MyEntity> MyEntities { get; set; } 
} 

myEntity所:

public class MyEntity 
{ 
    [Key] 
    public int Id { get; set; } 

    public string Name { get; set; } 
} 

我的主要方法是這樣的:

static void Main(string[] args) 
{ 
    using (MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Database=calibrationtest;Uid=calibration;Pwd=*******")) 
    { 
     using (MyContext context = new MyContext(conn)) 
     { 
      context.MyEntities.Add(new MyEntity() 
      { 
       Name = "1234" 
      }); 

      context.SaveChanges(); 
     } 
    } 
} 

當我運行它,我得到一個System.NotSupportedException:

Unable to determine the provider name for provider factory of type 
'MySql.Data.MySqlClient.MySqlClientFactory'. Make sure that the 
ADO.NET provider is installed or registered in the application config 

我試圖添加MySql.Data.My SqlClient.MySqlClientFactory到App.config中:

<provider invariantName="MySql.Data.MySqlClient.MySqlClientFactory" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"></provider> 

但後來我在Entity.Core錯誤抵達:

The 'Instance' member of the Entity Framework provider type 
'MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, 
Culture=neutral, PublicKeyToken=c5687fc88969c44d' did not return an object 
that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. 

我在做什麼錯?

編輯

我試圖消除的NuGet包(MySql.Data和MySql.Data.Entities.EF6)

然後我安裝了新版本directly from MySql現在上面的測試代碼成功運行?

回答

5

必須保留兩個配置部分, 這樣

<system.data> 
    <DbProviderFactories> 
     <remove invariant="System.Data.SQLite" /> <add name="SQLite Data Provider" description=".Net Framework Data Provider for SQLite" invariant="System.Data.SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> 
     <remove invariant="MySql.Data.MySqlClient" /><add name="MySQL" description="ADO.Net driver for MySQL" invariant="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/> 
    </DbProviderFactories> 
    </system.data> 

    <entityFramework> 
    <providers> 
     <provider invariantName="System.Data.SQLite"  type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq, Culture=neutral, PublicKeyToken=db937bc2d44ff139" /> 
     <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>  
    </providers> 
    </entityFramework> 
+0

這不會改變我的代碼中的任何內容。我已經編輯了我的文章,我是如何解決這個問題的。 – smerlung

0

我遇到了完全一樣的問題。 管理使用以下的NuGet包來解決它:

EntityFramework 6.1.0 
MySql.ConnectorNET.Data 6.8.3.2 
MySql.ConnectorNET.Entity 6.8.3.2 
1

我也收到此錯誤與EF6.1:「無法確定型「MySql.Data.MySqlClient的供應商工廠的供應商名稱。 MySqlClientFactory'確保ADO.NET提供程序已安裝或註冊在應用程序配置中

原來是因爲綁定重定向引用了老版本的MySQL.Data(6.7.4)不支持EF6。我將其更改爲:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
    <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> 
    <bindingRedirect oldVersion="0.0.0.0-6.8.3.0" newVersion="6.8.3.0" /> 
    </dependentAssembly> 
</assemblyBinding> 

錯誤消失了。