实体框架 6 不能使用 MySQL 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21206184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use a MySQL connection for entity framework 6
提问by Marc
I'm trying to use entity framework 6 with MySQL.
我正在尝试将实体框架 6 与 MySQL 一起使用。
I did install MySQL plugin for visual studio 1.1.1 and MySQL .Net connector 6.8.3.
我确实为 Visual Studio 1.1.1 和 MySQL .Net 连接器 6.8.3 安装了 MySQL 插件。
The problem is when I try to create a new ADO.NET Entity model, I can't choose MySQL for the connection.
问题是当我尝试创建一个新的 ADO.NET 实体模型时,我无法选择 MySQL 作为连接。
But if I create a connection from the connection tool in visual studio, I can see MySQL
但是如果我从visual studio中的连接工具创建一个连接,我可以看到MySQL
It means that the version currently installed is not compatible with entity framework 6. How is it possible after that I installed the latest connector and the plugin. Is there anything else to do?
这意味着当前安装的版本与实体框架6不兼容。 安装最新的连接器和插件后怎么可能。还有什么可做的吗?
Thank you
谢谢
EDIT
编辑
I did a fresh install of MySQL without installing the default connector and then I installed the 6.8.3 and the plugin for visual studio.
我在没有安装默认连接器的情况下全新安装了 MySQL,然后我安装了 6.8.3 和 Visual Studio 插件。
Then I can see the connection I made for my Entity model but when I'm about to choose the entity version, I get this message.
然后我可以看到我为我的实体模型建立的连接,但是当我要选择实体版本时,我收到了这条消息。
采纳答案by Marc
LATEST EDIT
最新编辑
The bug has been fixed.
该错误已被修复。
Chris' comment:
克里斯的评论:
2015-11-07 and you can now get it all working without editing any files or doing weird stuff. Use the MySQL for Windows installer and include the Visual Studio support and the latest Connector.Net. Be sure to update after installing and you will get the latest of each (1.2.5 and 6.9.8). Use NuGet to install EntityFramework, MySql.Data, and MySql.Data.Entity. Finally, built and enjoy code-first reverse engineering goodness by adding an Ado.Net Entity Model.
2015 年 11 月 7 日,您现在可以在不编辑任何文件或做奇怪的事情的情况下完成所有工作。使用 MySQL for Windows 安装程序并包含 Visual Studio 支持和最新的 Connector.Net。请务必在安装后进行更新,您将获得最新版本(1.2.5 和 6.9.8)。使用 NuGet 安装 EntityFramework、MySql.Data 和 MySql.Data.Entity。最后,通过添加 Ado.Net 实体模型构建并享受代码优先逆向工程的优点。
Original answer
原答案
I found out it's a bug from MySQL.
我发现这是 MySQL 的一个错误。
Here'sthe link explaining a workarround.
这是解释解决方法的链接。
On your machine where VS 2013 is installed, VS plugin (1.1.3 GA) and Connector/Net
Close all VS instances before doing the steps.
On a Windows Explorer window go to this path or wherever you installed you Connector/net binaries
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\
Copy the file:
MySql.Data.Entity.EF6.dll
And paste it to this folder
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies
If it asks you to overwrite it please do so.
You'll need admin rights in order to overwrite the file.
Then you can try again to generate the script for your model.
It is important that you have the 1.1.3 version of the VS plugin installed since this workaround is for that.
在安装了 VS 2013 的机器上,VS 插件 (1.1.3 GA) 和 Connector/Net
在执行这些步骤之前关闭所有 VS 实例。
在 Windows 资源管理器窗口中,转到此路径或安装连接器/网络二进制文件的任何位置
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\
复制文件:
MySql.Data.Entity.EF6.dll
并将其粘贴到此文件夹中
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies
如果它要求您覆盖它,请这样做。
您需要管理员权限才能覆盖文件。
然后您可以再次尝试为您的模型生成脚本。
安装 VS 插件的 1.1.3 版本很重要,因为此解决方法适用于此。
Unfortunately it doesn't work for me, so I downgraded to entity framework 5 until they fix this.
不幸的是它对我不起作用,所以我降级到实体框架 5,直到他们解决这个问题。
EDIT
编辑
Finaly, it works now.
最后,它现在可以工作了。
I had to add the 3 following DLLs :
我必须添加以下 3 个 DLL:
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.Entity.EF6.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Web.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Data.Entity.EF6.dll
- C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5\MySql.Web.dll
Then I changed the EntityFramework part in the web config to :
然后我将 web 配置中的 EntityFramework 部分更改为:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, 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" />
</providers>
</entityFramework>
Don't forget to REBUILDand you should be able to create a entity framework 6 model with MySQL.
不要忘记REBUILD,您应该能够使用 MySQL 创建实体框架 6 模型。
IMPORTANT
重要的
Make sure you have installed MySQL for visual studio 1.1.3and MySQL connector .net 6.8.3
确保您已为 Visual Studio 1.1.3和 MySQL 连接器 .net 6.8.3安装 MySQL
回答by Edvaldo Silva
SOLUTION! 2017
解决方案!2017年
Install Nuget Package:
安装 Nuget 包:
Install-Package EntityFramework
Install-Package MySql.Data.Entity -Version 6.9.9
Install MySQL for Visual Studio 1.2.6 ? - https://dev.mysql.com/downloads/windows/visualstudio/
为 Visual Studio 1.2.6 安装 MySQL 吗?- https://dev.mysql.com/downloads/windows/visualstudio/
Changes in Web.Config
Web.Config 中的更改
<EntityFramework>
to:
到:
<EntityFramework codeConfigurationType = "MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
Add (** your information **):
添加(**您的信息**):
<connectionStrings>
<add name="**YourContextName**" connectionString="server=**xxx.xxx.xxx.xxx**;port=3306;user id=**your user**;password=**your password**;database=**your database**" providerName="MySql.Data.MySqlClient" /></connectionStrings>
Restart Visual Studio
重新启动 Visual Studio
My Settings:
- Microsoft Visual Studio Community 2015
- Dot Net Framework 4.5.2
- Asp.Net MVC 5.2.3.0
- MySql Server 5.6
我的设置:
- Microsoft Visual Studio Community 2015
- Dot Net Framework 4.5.2
- Asp.Net MVC 5.2.3.0
- MySql Server 5.6
回答by awerdan
Not sure if this is still a problem for you, guys, but none of the above solutions worked for me... until I found this:
不知道这对你们来说是否仍然是一个问题,伙计们,但上述解决方案都不适合我......直到我发现这个:
http://www.microsoft.com/en-us/download/details.aspx?id=40762
http://www.microsoft.com/en-us/download/details.aspx?id=40762
After applying the patches (for both VS2013 and VS2012), I got VS2013 working wonderfully with EF6 for MySql.
应用补丁(针对 VS2013 和 VS2012)后,我让 VS2013 与用于 MySql 的 EF6 完美配合。
ps. Package versions installed (from NuGet):
EntityFramework - 6.1.3 (03/10/2015)
MySqlData - 6.9.6 (02/24/2015)
MySql.Data.Entity - 6.9.6 (02/24/2015)
MySql.Web - 6.9.6 (02/24/2015)
附:安装的包版本(来自 NuGet):
EntityFramework - 6.1.3 (03/10/2015)
MySqlData - 6.9.6 (02/24/2015)
MySql.Data.Entity - 6.9.6 (02/24/2015)
MySql。网络 - 6.9.6 (02/24/2015)
NuGet does the job right and updates Web.Config accordingly, filling the "entityFramework" section with whatever needed.
NuGet 可以正确地完成工作并相应地更新 Web.Config,用所需的任何内容填充“entityFramework”部分。
For VS2013, it simply goes from "Not Working at all" to "Fully functional". For VS2012, it makes it possible to use "Code First from Database", which was not available before applying the patch.
对于 VS2013,它只是从“根本不工作”变为“功能齐全”。对于 VS2012,它可以使用“Code First from Database”,这在应用补丁之前是不可用的。
I wish I had found this about 10 days ago, so hope this may still be of some help.
我希望我在大约 10 天前找到了这个,所以希望这仍然有帮助。
回答by Shyamal Parikh
For me reinstalling MySQL and using the installed connector version as a reference in Visual studio worked!
对我来说,重新安装 MySQL 并使用已安装的连接器版本作为 Visual Studio 中的参考工作!
Steps:
脚步:
- Reinstall MySQL Tools with the latest stable MySQL connector for windows.
Add this 3 files as reference in Visual Studio:
MySql.Data.dll
- MySql.Data.Entity.EF6.dll
MySql.Web.dll
Modify following Web.config from
<providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers>
- 使用适用于 Windows 的最新稳定 MySQL 连接器重新安装 MySQL 工具。
在 Visual Studio 中添加这 3 个文件作为参考:
MySql.Data.dll
- MySql.Data.Entity.EF6.dll
MySql.Web.dll
修改以下 Web.config 来自
<providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers>
To:
到:
<providers>
<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" />
</providers>
Make sure your connection username/password/db name are correct or Visual Studio may crash. (Happened with me.)
确保您的连接用户名/密码/db 名称正确,否则 Visual Studio 可能会崩溃。(发生在我身上。)
回答by Chris
2015-11-07 Update
2015-11-07 更新
The bugs are fixed and you can now get it all working without editing any files or even referencing assemblies outside of NuGet. You simply have to install the right thing and add the right NuGet packages. Here's how:
这些错误已得到修复,您现在无需编辑任何文件,甚至无需引用 NuGet 之外的程序集,即可让其全部工作。您只需要安装正确的东西并添加正确的 NuGet 包。就是这样:
Use the MySQL for Windows installer and include the Visual Studio support and the latest Connector.Net. Be sure to update after installing and you will get the latest of each (1.2.5 and 6.9.8).
使用 MySQL for Windows 安装程序并包含 Visual Studio 支持和最新的 Connector.Net。请务必在安装后进行更新,您将获得最新版本(1.2.5 和 6.9.8)。
Then, use NuGet to install EntityFramework, MySql.Data, and MySql.Data.Entity.
然后,使用 NuGet 安装 EntityFramework、MySql.Data 和 MySql.Data.Entity。
Finally, build and enjoy code-first reverse engineering goodness by adding an Ado.Net Entity Model.
最后,通过添加 Ado.Net 实体模型构建并享受代码优先逆向工程的优点。
回答by Hans Vader
i found this on the mySQL Bucktracker
我在mySQL Bucktracker上找到了这个
Try the following workaround....
Run the following command on the MySQL DB and then try if this works.
set global optimizer_switch='derived_merge=off'
尝试以下解决方法......
在 MySQL 数据库上运行以下命令,然后尝试是否有效。
设置全局 optimizer_switch='derived_merge=off'
I also added the following assemblies:
我还添加了以下程序集:
- EF6 as Nuget package
- MySQL.Data
- MySql.Data.Entity.EF6.dll
- MySQL.Web (not sure if really needed)
- EF6 作为 Nuget 包
- MySQL.Data
- MySql.Data.Entity.EF6.dll
- MySQL.Web(不确定是否真的需要)
回答by quetzalcoatl
FYI for everyone using a multi-project solution, if you can't run your GUI project due to problems with finding/loading MySql EF6 provider, like:
仅供使用多项目解决方案的每个人使用,如果由于查找/加载 MySql EF6 提供程序时出现问题而无法运行 GUI 项目,例如:
No Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.(...)'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882for more information.
未找到具有不变名称“MySql.Data.(...)”的 ADO.NET 提供程序的实体框架提供程序。确保提供程序已在应用程序配置文件的“entityFramework”部分注册。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882。
please see this question.
请看这个问题。
Although it's about SqlServer the problem might be the same: if only one of your projects handles the DB operations, then most probably you added Nuget References to MySql.Data
and MySql.Data.Entity
and EntityFramework
to that project. Since MySql.Data
and EntityFramework
are used directly by the code, the build process is smart enough to copy them to output folder, and to copy it second time to the GUI project's output folder as well.
虽然它是关于 SqlServer 的,但问题可能是相同的:如果只有一个项目处理数据库操作,那么很可能你向该项目添加了 Nuget 引用MySql.Data
和MySql.Data.Entity
和和EntityFramework
。由于MySql.Data
和EntityFramework
由代码直接使用,因此构建过程足够智能,可以将它们复制到输出文件夹,并再次将其复制到 GUI 项目的输出文件夹。
However, since MySql.Data.Entity
is referred to only by the app.config
or web.config
text file, the build process thinks that this reference is not needed (the codedoes not use it!) and strips it away, and you won't see MySql.Data.Entity.EF6.dll
in GUI project output dir, and this causes the GUI project to be unable to find that provider.
但是,由于MySql.Data.Entity
仅由app.config
orweb.config
文本文件引用,因此构建过程认为不需要此引用(代码不使用它!)并将其剥离,您将不会MySql.Data.Entity.EF6.dll
在 GUI 项目输出目录中看到,并且这会导致 GUI 项目无法找到该提供程序。
So - two options - ensure that something copies that DLL there as a part of build process (i.e. pre/post build event, deployment package builder, CI task, human task, whatever), or just make a direct reference to the assembly in your DB/EF6 project.
所以 - 两个选项 - 确保某些东西在那里复制该 DLL 作为构建过程的一部分(即前/后构建事件、部署包构建器、CI 任务、人工任务等),或者只是直接引用您的程序集DB/EF6 项目。
回答by Geovanny López
My was facing the same issue.
我的面临同样的问题。
I did the following:
1. Install MySQL for Visual Studio from:
https://dev.mysql.com/downloads/windows/visualstudio/
2. Install MySql.Data in my project
3. Install MySql.Data.EntityFramework in my project
我执行了以下操作:
1.从以下位置
为 Visual Studio 安装 MySQL:
https:
//dev.mysql.com/downloads/windows/visualstudio/ 2. 在我的项目中
安装 MySql.Data 3.在我的项目中
安装 MySql.Data.EntityFramework
回答by To delete profile
Did you try to install the latest dot net connector for my sql ? http://dev.mysql.com/tech-resources/articles/mysql-installer-for-windows.htmlor http://dev.mysql.com/downloads/connector/net/depending your context.
您是否尝试为我的 sql 安装最新的 dot net 连接器? http://dev.mysql.com/tech-resources/articles/mysql-installer-for-windows.html或 http://dev.mysql.com/downloads/connector/net/取决于您的上下文。