作爲一個EF noob,我試圖使用實體框架6 Code First與MySql Server 5.6,我安裝在我的開發計算機上。MySql連接器EF6
我做了一個非常小的測試控制檯項目。我已經添加了的NuGet包:
- 的EntityFramework 6.0.2
- MySql.Data
- 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現在上面的測試代碼成功運行?
這不會改變我的代碼中的任何內容。我已經編輯了我的文章,我是如何解決這個問題的。 – smerlung