Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 5.0 code-first with MySQL Connector 6.6.5.0 on .Net 4.5
    primarykey
    data
    text
    <p>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 (<code>MySql.Data</code> reference) and MySql Entity 6.5.4.0 (<code>MySql.Data.Entity</code> 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.</p> <p>Here's what I have on app.config</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt; &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt; &lt;/configSections&gt; &lt;startup&gt; &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt; &lt;/startup&gt; &lt;system.data&gt; &lt;DbProviderFactories&gt; &lt;add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" /&gt; &lt;/DbProviderFactories&gt; &lt;/system.data&gt; &lt;connectionStrings&gt; &lt;add name="MyConnectionName" connectionString="Server=mysql13.myserver.com.br;Database=mydb;User Id=username;Pwd=pa$$w0rd;" providerName="MySql.Data.MySqlClient" /&gt; &lt;/connectionStrings&gt; &lt;entityFramework&gt; &lt;defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"&gt; &lt;/defaultConnectionFactory&gt; &lt;/entityFramework&gt; &lt;runtime&gt; &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-6.6.5.0" newVersion="6.6.5.0" /&gt; &lt;/dependentAssembly&gt; &lt;/assemblyBinding&gt; &lt;/runtime&gt; &lt;/configuration&gt; </code></pre> <p>In the code</p> <p>User class</p> <pre><code>public class User { public int Id { get; set; } public string Name { get; set; } public virtual List&lt;Permission&gt; Permissions { get; set; } public User() { Permissions = new List&lt;Permission&gt;(); } } public class Permission { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public class UserContext : DbContext { public DbSet&lt;User&gt; Users { get; set; } public DbSet&lt;Permission&gt; Permissions { get; set; } } </code></pre> <p>Creating a new record</p> <pre><code> 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(); } </code></pre> <p>And I get the exception at the line <code>db.Users.Add(user);</code></p> <pre><code>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.&lt;.ctor&gt;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.&lt;.ctor&gt;b__1() InnerException: </code></pre> <p>I've tried several things, including adding <code>Culture=neutral, PublicKeyToken=c5687fc88969c44d</code> in the <code>DbProviderFactories</code> section to no avail..</p> <p>I added the Entity Framework, MySql Data Connector and MySql.Data.Entity using NuGet Package Manager.</p> <p>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.</p> <p>Has anyone made this work? Can you post both the app.config AND the code to make it work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload