gpt4 book ai didi

MySql 连接器 EF6

转载 作者:可可西里 更新时间:2023-11-01 06:32:52 34 4
gpt4 key购买 nike

作为 EF 菜鸟,我正在尝试将 Entity Framework 6 Code First 与我安装在我的开发计算机上的 MySql Server 5.6 结合使用。

我做了一个非常小的测试控制台项目。我已经添加了 NuGet 包:

  1. Entity Framework 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有两个类:

我的语境:

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

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

我的实体:

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.MySqlClient.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现在上面的测试代码运行成功了吗?

最佳答案

你必须保留两个配置部分,像这样

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

关于MySql 连接器 EF6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20992057/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com