gpt4 book ai didi

c# - MySql 连接字符串的 Entity Framework 错误

转载 作者:行者123 更新时间:2023-11-29 10:56:23 25 4
gpt4 key购买 nike

我正在用 Entity Framework 在cmd中做一个应用程序来研究,我正在使用MySql数据库,数据库已经创建并连接到Visual Studio,但我无法打开该应用程序,目前很简单,在下面应用程序代码和错误:

RdiContext.cs

using rdi_musica.core;
using System.Data.Entity;

namespace rdi_musica.infra
{
public class RdiContext : DbContext
{
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<Genero> Generos { get; set; }
public DbSet<Banda> Bandas { get; set; }
public DbSet<Musica> Musicas { get; set; }

}
}

BandaRepository.cs

using rdi_musica.core;
using System.Linq;

namespace rdi_musica.infra
{
public class BandaRepository
{
RdiContext context = new RdiContext();
public Banda getBandaById(int Id)
{
var b = context.Bandas.Where(x => x.Id == Id).Select(x => x).FirstOrDefault();
return b;
}
}
}

应用程序配置

<?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>
<connectionStrings>
<add name="Default" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=redeinova_musica;uid=root;password=root"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<!--defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /-->
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
<!--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, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider-->
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>

错误:

enter image description here

转录错误:

The Entity Framework provider type 
'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6'
registered in the application config file for the ADO.NET provider with
invariant name 'MySql.Data.MySqlClient' could not be loaded.

项目结构:

enter image description here

Ps:是的,我安装了MySql驱动程序并添加了引用

enter image description here

最佳答案

乍一看,您必须在 DbContext 类上设置 DbConfigurationTypeAttribute 才能使用 MySQL:

namespace rdi_musica.infra
{
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class RdiContext : DbContext
{
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<Genero> Generos { get; set; }
public DbSet<Banda> Bandas { get; set; }
public DbSet<Musica> Musicas { get; set; }

}
}

注意:在 MySQL 连接器版本低于 6.8.x 的 EF 5 中,您的数据上下文设置似乎工作正常,但在 MySQL 连接器版本 6.8.x 及更高版本的 EF 6 中,您需要显式设置 DbConfigurationType使用 MySql.Data.Entity 命名空间。

如果仍然不够(即抛出相同的异常),请尝试将 codeConfigurationType 添加到 web.config 中:

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguratio‌​n, MySql.Data.Entity.EF6">
...
</entityFramework>

然后删除 system.data 元素内的 DbProviderFactories 部分:

<!-- taken from /a/21954322 by Donald Jansen -->
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>

并将 entityFramework 元素内的提供程序设置修改为:

<!-- taken from /a/21954322 by Donald Jansen -->
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguratio‌​n, MySql.Data.Entity.EF6">
...
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>

引用:

Entity Framework Config File Settings

Entity Framework Code-Based Configuration (MSDN)

类似问题:

No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider

Enity Framework With MySQL

关于c# - MySql 连接字符串的 Entity Framework 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958761/

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