实体框架5.0代码首先与MySQL连接器6.6.5.0 on .Net 4.5 [英] Entity Framework 5.0 code-first with MySQL Connector 6.6.5.0 on .Net 4.5

查看:123
本文介绍了实体框架5.0代码首先与MySQL连接器6.6.5.0 on .Net 4.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的生活,我无法让我的C#WinApp使用MySql连接器6.6.5.0( MySql.Data )与MySql数据库一起使用Entity Framework 5.0参考)和MySql Entity 6.5.4.0( MySql.Data.Entity 引用)。在Visual Studio 2012上使用.Net Framework 4.5。在撰写本文时,上述版本都是最新版本。



这是我在app.config上

 <?xml version =1.0encoding =utf-8?> 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< startup>
< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.5/>
< / startup>
< system.data>
< DbProviderFactories>
< add name =MySQL数据提供者
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>
<运行时>
< assemblyBinding xmlns =urn:schemas-microsoft-com:asm.v1>
< dependentAssembly>
< assemblyIdentity name =MySql.DatapublicKeyToken =c5687fc88969c44dculture =neutral/>
< bindingRedirect oldVersion =0.0.0.0-6.6.5.0newVersion =6.6.5.0/>
< / dependentAssembly>
< / assemblyBinding>
< / runtime>
< / configuration>

在代码



用户类

  public class User 
{
public int Id {get;组; }
public string Name {get;组; }
public virtual List< Permission>权限{get;组; }

public User()
{
Permissions = new List< Permission>();
}
}

public class Permission
{
public int Id {get;组; }
public string Name {get;组; }
public string描述{get;组; }
}

public class UserContext:DbContext
{
public DbSet< User>用户{get;组; }
public DbSet< Permission>权限{get;组;
}

创建新记录


$ b $使用(var db = new UserContext())
{
权限permission1 = new Permission(){Name =权限1,描述= desc 1};
权限permission2 = new Permission(){Name =2nd Perm,描述=Desc2};
用户user = new User(){Name =Joao};
user.Permissions.Add(permission1);
user.Permissions.Add(permission2);

db.Users.Add(user);
db.SaveChanges();
}

我在 db的行上收到异常。 User.Add(user);

  System.InvalidOperationException未处理
HResult = 2146233079
消息=无法将Database.DefaultConnectionFactory设置为应用程序配置中指定的MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data类型的实例。查看内部异常的详细信息。
Source = EntityFramework
StackTrace:
在System.Data.Entity.Internal.AppConfig。< .ctor> b__1()
在System.Lazy`1.CreateValue()
---从前一个位置抛出异常的堆栈跟踪结束---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在System.Lazy`1.get_Value( )
在System.Data.Entity.Internal.AppConfig.get_DefaultConnectionFactory()
[删除堆栈的其余部分]
InnerException:System.InvalidCastException
HResult = -2147467262
消息=无法将类型为MySql.Data.MySqlClient.MySqlClientFactory的对象转换为键入System.Data.Entity.Infrastructure.IDbConnectionFactory。
Source = EntityFramework
StackTrace:
在System.Data.Entity.Internal.AppConfig。< .ctor> b__1()
InnerException:

我尝试了几件事,包括添加 Culture = neutral,PublicKeyToken = c5687fc88969c44d DbProviderFactories 部分无效..



我添加了实体框架,MySql数据连接器和MySql.Data使用NuGet软件包管理器的实体。



我看到很多其他类似问题的帖子,但找不到明确的解决方案,尤其是与版本控制组合EF 5 + MySql连接器6.6.5.0。



有没有人做这个工作?你可以发布app.config和代码来使其工作吗?

解决方案

MySQL连接器6.6.5仅支持实体框架4.3如 here 所述。我亲自使用它迄今为止工作良好。但是,如果您需要专门的实体框架5,则需要使用MySQL Connector 6.6.7 Beta,现在支持它,如此处。我没有尝试过6.6.7版。



更新1:您可以使用MySQL连接器6.6找到使用EF 4.3代码的博客帖子 here



更新2:示例.NET 4.5控制台应用程序使用EF 4.3和MySql连接器6.6.5 此处


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?

解决方案

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.

这篇关于实体框架5.0代码首先与MySQL连接器6.6.5.0 on .Net 4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆