gpt4 book ai didi

.net - Entity Framework 5.0 代码优先,在 .Net 4.5 上使用 MySQL Connector 6.6.5.0

转载 作者:行者123 更新时间:2023-12-03 02:01:25 25 4
gpt4 key购买 nike

在我的一生中,我无法让我的 C# WinApp 使用 MySql 连接器 6.6.5.0(MySql.Data 引用)和 MySql Entity 6.5 与带有 MySql 数据库的 Entity Framework 5.0 配合使用.4.0(MySql.Data.Entity 引用)。在 Visual Studio 2012 上使用 .Net Framework 4.5。在撰写本文时,上述版本都是最新的稳定版本。

这是我在 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>

在代码中

用户类别

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; }
}

创建新记录

        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();
}

我在 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:

我尝试了多种方法,包括在 DbProviderFactories 部分添加 Culture=neutral, PublicKeyToken=c5687fc88969c44d 但无济于事。

我使用 NuGet Package Manager 添加了 Entity Framework 、MySql 数据连接器和 MySql.Data.Entity。

我看过很多其他帖子也有类似的问题,但找不到明确的解决方案,特别是使用版本控制组合 EF 5 + MySql Connector 6.6.5.0。

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

最佳答案

如上所述,MySQL Connector 6.6.5 仅支持 Entity Framework 4.3 here 。我亲自使用过它,到目前为止效果很好。但是,如果您特别需要 Entity Framework 5,则需要使用 MySQL Connector 6.6.7 Beta,它现在支持它,如上所述 here 。不过我还没有尝试过 6.6.7 版。

更新 1:您可以找到使用 EF 4.3 code-first 与 MySQL Connector 6.6 的博客文章 here .

更新 2:使用 EF 4.3 和 MySql Connector 6.6.5 的示例 .NET 4.5 控制台应用程序 here .

关于.net - Entity Framework 5.0 代码优先,在 .Net 4.5 上使用 MySQL Connector 6.6.5.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com