Reputation: 205
I'm doing an application in cmd with entity framework to study, I'm using MySql database with the database already created and connected to visual studio, but I can not open the application, which is simple for now, below the application codes And the error:
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>
And the error:
Error transcription:
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.
The project structure:
Ps: Yes, I installed the MySql drivers and added the references
Upvotes: 0
Views: 3593
Reputation: 24957
From first glance seems that you must set DbConfigurationTypeAttribute
on DbContext
class to use 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; }
}
}
NB: In EF 5 with MySQL connector version below 6.8.x your data context setup seems working fine, but in EF 6 with MySQL connector version 6.8.x and above you need to explicitly set DbConfigurationType
to use MySql.Data.Entity
namespace.
If it's still not enough (i.e. throwing same exception), try adding codeConfigurationType
into web.config:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
...
</entityFramework>
Then remove part of DbProviderFactories
inside system.data
element:
<!-- 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>
And modify provider setting inside entityFramework
element to this:
<!-- taken from /a/21954322 by Donald Jansen -->
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, 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>
Reference:
Entity Framework Config File Settings
Entity Framework Code-Based Configuration (MSDN)
Similar issues:
No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider
Upvotes: 1