For the life of me, I can't get my C# WinApp to work with Entity Framework 5.0 with a MySql database using MySql connector 6.6.5.0 (MySql.Data
reference) and MySql Entity 6.5.4.0 (MySql.Data.Entity
reference). Using .Net Framework 4.5 on Visual Studio 2012. At the time of this writing, the versions above are all the latest stable ones.
Here's what I have on 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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MyConnectionName"
connectionString="Server=mysql13.myserver.com.br;Database=mydb;User Id=username;Pwd=pa$$w0rd;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data">
</defaultConnectionFactory>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.6.5.0" newVersion="6.6.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
In the code
User class
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Permission> Permissions { get; set; }
public User()
{
Permissions = new List<Permission>();
}
}
public class Permission
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Permission> Permissions { get; set; }
}
Creating a new record
using (var db = new UserContext())
{
Permission permission1 = new Permission() { Name = "Permission 1", Description = "The desc 1" };
Permission permission2 = new Permission() { Name = "2nd Perm", Description = "Desc2" };
User user = new User() { Name = "Joao" };
user.Permissions.Add(permission1);
user.Permissions.Add(permission2);
db.Users.Add(user);
db.SaveChanges();
}
And I get the exception at the line db.Users.Add(user);
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Failed to set Database.DefaultConnectionFactory to an instance of the 'MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data' type as specified in the application configuration. See inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.AppConfig.get_DefaultConnectionFactory()
[removing the rest of the stack]
InnerException: System.InvalidCastException
HResult=-2147467262
Message=Unable to cast object of type 'MySql.Data.MySqlClient.MySqlClientFactory' to type 'System.Data.Entity.Infrastructure.IDbConnectionFactory'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
InnerException:
I've tried several things, including adding Culture=neutral, PublicKeyToken=c5687fc88969c44d
in the DbProviderFactories
section to no avail..
I added the Entity Framework, MySql Data Connector and MySql.Data.Entity using NuGet Package Manager.
I've seen many other posts with a similar problem, but can't find a clear solution, especially with the versioning combo EF 5 + MySql Connector 6.6.5.0.
Has anyone made this work? Can you post both the app.config AND the code to make it work?
Most of my production web.config. I've taken out all the company specific stuff... Also this config was setup for MVC4/EF5/OData 5.2 with IIS compression. Pretty complicated stuff...most of it won't be required. But I figured you wanted my raw config for reference.
Sounds like there's some strange assembly resolution issues. Try deleting the
assemblyBinding
element in your config file and then run NuGet'sAdd-BindingRedirect
command.I ran into the same problem in my first day of using mysql in EF. And I found your config is incorrect, and it should be
And make sure you use .net framework 4.5 and mysql connector lib for .net 4.5 as well for EF 5, since the connector for .net 4 supports only EF 4.4.
The MySQL Connector 6.6.5 only supports Entity Framework 4.3 as mentioned here. I have personally used it which worked well so far. However, if you require Entity Framework 5 specifically, you would need to use MySQL Connector 6.6.7 Beta which now supports it as mentioned here. I haven't tried v 6.6.7 though.
Update 1: You can find the blog post of using EF 4.3 code-first with MySQL Connector 6.6 here.
Update 2: Sample .NET 4.5 console application using EF 4.3 and MySql Connector 6.6.5 here.