我正在cmd中使用实体框架进行应用程序研究,我正在使用已创建的数据库和连接到Visual Studio的MySql数据库,但是我无法打开应用程序,这在目前很简单,在应用程序代码和错误:
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;
}
}
}
的App.config
<?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>
错误:
错误转录:
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.
项目结构:
Ps:是的,我安装了MySql驱动程序并添加了引用
答案 0 :(得分:1)
从第一眼看来,您必须在DbConfigurationTypeAttribute
类上设置DbContext
才能使用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
以使用{ {1}}命名空间。
如果仍然不够(即抛出同样的异常),请尝试将MySql.Data.Entity
添加到web.config中:
codeConfigurationType
然后移除<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
...
</entityFramework>
元素中的部分DbProviderFactories
:
system.data
将<!-- 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
参考:
Entity Framework Config File Settings
Entity Framework Code-Based Configuration (MSDN)
类似问题:
No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider