简体   繁体   English

Visual Studio 2017 未在连接列表中显示 MySQL

[英]Visual Studio 2017 not showing MySQL in connection list

I am using "Visual Studio 2017 Professional".我正在使用“Visual Studio 2017 Professional”。 I can not get a project to recognize that MySQL is an available DB option.我无法让项目认识到 MySQL 是一个可用的数据库选项。 MySQL does not show up in the list of possible connections when I create a new model.当我创建一个新模型时,MySQL 没有出现在可能的连接列表中。 I know that there are tons of SO questions and other web pages on this topic because I have tried a lot of the suggestions.我知道有很多关于这个主题的 SO 问题和其他网页,因为我已经尝试了很多建议。 Each is subtlety different in cause and solution.每一个在原因和解决方案上都有微妙的不同。 None of the solutions seem to work for me.似乎没有一个解决方案对我有用。

I have tried the following with VS 2017 and 2015 with the same results.我在 VS 2017 和 2015 中尝试了以下操作,结果相同。 I have older versions of VS available but it didn't seem worthwhile to march that far back.我有旧版本的 VS 可用,但向后推进似乎不值得。 I have also tried installing the latest and older versions of MySQL both with the "Community Installer" and the standalone msi's when necessary.我还尝试在必要时使用“社区安装程序”和独立的 msi 安装最新和旧版本的 MySQL。 I went back to the "latest" pre-6.10 version of MySQL.我回到了 MySQL 6.10 之前的“最新”版本。

To begin, this saga started last week when I noticed that nuget had MySQL package updates available.首先,当我注意到 nuget 有可用的 MySQL 包更新时,这个传奇开始了。 I applied them and then went to upgrade the installed MySQL componants with the Community Installer.我应用了它们,然后使用社区安装程序升级已安装的 MySQL 组件。 The nuget changes did apply. nuget 更改确实适用。 The Community Installer could not uninstall MySQL for VS though.但是,社区安装程序无法为 VS 卸载 MySQL。 The system installer could not uninstall it.系统安装程序无法卸载它。 Rebooting and power-cycling did nothing.重新启动和电源循环没有任何作用。

On the advice of our local tech support folks I used a third party uninstaller that turned out to be more aggressive than we imagined.在我们当地技术支持人员的建议下,我使用了第三方卸载程序,结果证明它比我们想象的更具攻击性。 It seems to have randomly stommped across the registry erasing whatever it found.它似乎随机踩踏了注册表,擦除了它发现的任何内容。 Long story short(er), I ended up having my machine reimaged.长话短说(呃),我最终让我的机器重新成像。 So, I have a fresh windows install.所以,我有一个全新的 Windows 安装。

Given the fresh Win10 platform, I installed the various things I normally use including the VS versions I typically use.鉴于新的 Win10 平台,我安装了我通常使用的各种东西,包括我通常使用的 VS 版本。 I installed the latest Community MySQL installer.我安装了最新的社区 MySQL 安装程序。 I did a "custom" MySQL install because I don't need a server on this device or fabric or many other things.我进行了“自定义”MySQL 安装,因为我不需要此设备或结构上的服务器或许多其他东西。

I finished the basic reinstall of everything then rebooted just to be sure.我完成了一切的基本重新安装然后重新启动只是为了确定。 I started VS 2017 and created a new web app with the latest available version of .Net (4.6.1).我启动了 VS 2017 并使用最新的 .Net (4.6.1) 版本创建了一个新的 Web 应用程序。 I applied nuget updates and added EntityFramework and MySQL.Data.我应用了 nuget 更新并添加了 EntityFramework 和 MySQL.Data。 I then tried to create a new "code first" model.然后我尝试创建一个新的“代码优先”模型。

I can get to the point where I am asked to create a connection.我可以达到要求我创建连接的地步。 When I click the button the list of available DB providers only shows MS SQL.当我单击按钮时,可用数据库提供程序的列表仅显示 MS SQL。

I have seen the articles that say to add lines to Web.config and I have seen the MySQL .Net Connector docs that say the same things.我看过那些说要在Web.config添加行的文章,我也看过 MySQL .Net Connector 文档也说过同样的话。 I can see the MySQL entries in Web.config but they do not show up when creating a connection.我可以在Web.config看到 MySQL 条目,但在创建连接时它们不显示。

An oddity is that I can go to Tools -> Connect to Database and see EVERY DB type my machine knows about including MySQL.奇怪的是,我可以转到“ Tools -> Connect to Database并查看我的机器知道的关于包括 MySQL 的每个数据库类型。 I can create the connection string and it works.我可以创建连接字符串并且它可以工作。 I can see the DB in "Server Explorer` but that doesn't make it available to code.我可以在“服务器资源管理器”中看到数据库,但这并不能用于代码。

Ok, I have followed the above steps over and over including downgrading the version of EntityFramework as well as the MySQL version.好的,我一遍又一遍地遵循上述步骤,包括降级 EntityFramework 的版本以及 MySQL 版本。

My most recent attempt has me back to the latest version of VS and MySQL.我最近的尝试让我回到了最新版本的 VS 和 MySQL。 The relevant (I think) section of the Web.config file follow. Web.config文件的相关(我认为)部分如下。 Note the defaultConnectionFactory entry is LocalDbConnectionFactory .注意defaultConnectionFactory条目是LocalDbConnectionFactory I have changed that to SqlConnectionFactory but it has not made a difference.我已将其更改为SqlConnectionFactory但它没有任何区别。 I have also changed the order of the providers and the order of the major blocks.我还更改了提供程序的顺序和主要块的顺序。

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <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, Version=6.10.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider>
    </providers>
</entityFramework>
<system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add description=".Net Framework Data Provider for MySQL" invariant="MySql.Data.MySqlClient"
           name="MySQL Data Provider"
           type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

Edit编辑

My current impression is that VS 2017 is an unusable mess masquerading as a released, salable product.我目前的印象是 VS 2017 是一个无法使用的混乱,伪装成已发布的、可销售的产品。 I have returned to 2015.我回到了2015年。

I have come closer to getting this to work in 2015. I have the latest MySQL items for VS and .Net installed.我已经更接近于在 2015 年使用它。我安装了用于 VS 和 .Net 的最新 MySQL 项目。 I have downgraded the nuget EF package to 6.0 and the MySql packages to 6.8.8.我已将 nuget EF 包降级到 6.0,将 MySql 包降级到 6.8.8。

In, what I am now calling, "the good old days" I could add a new model to an MVC project by selecting the Models folder;在我现在所说的“过去的美好时光”中,我可以通过选择 Models 文件夹向 MVC 项目添加一个新模型; selecting "Add New Item";选择“添加新项目”; choosing ADO.Net model;选择ADO.Net模型; select a connection option (MySQL);选择一个连接选项(MySQL); enter the connection info;输入连接信息; choose DB First or Code First and wait for the scaffolding fun to finish.选择 DB First 或 Code First,然后等待脚手架的乐趣完成。

Now, I have to first go to Tools -> Connect to Database and create a conenction to a MySql DB.现在,我必须首先转到“ Tools -> Connect to Database并创建一个Tools -> Connect to Database MySql 数据库的连接。 If I do not do this I will not see MySQL in the list of possible connections later.如果我不这样做,我以后就不会在可能的连接列表中看到 MySQL。 I then clean and build the project.然后我清理并构建项目。 Next i try to create the model as above.接下来我尝试像上面那样创建模型。 Noe I can see the MySQL connection option and I can enter the DB creds and test the connection.不,我可以看到 MySQL 连接选项,我可以输入数据库凭据并测试连接。

The next step is to choose DB First or Code First.下一步是选择 DB First 或 Code First。 In either case, clicking the "next" button flashes a blank pop-up that then vanishes.在任何一种情况下,单击“下一步”按钮都会闪烁一个空白弹出窗口,然后消失。 No errors are shown and no connection is made.未显示任何错误且未建立任何连接。

It was never this hard in the past.过去从来没有这么难过。

I have tried a lot of different MySQL and EF versions on 2015 and 2017 at this point.此时,我在 2015 年和 2017 年尝试了很多不同的 MySQL 和 EF 版本。 If there is a magic combo please tell me.如果有魔法组合请告诉我。

I have been fighting this fight for two weeks.我已经为这场斗争打了两个星期。 I FINALLY found a combo of versions that works for me.终于找到了适合我的版本组合。 The following was applied to a pristine re-image of my Win10 desktop two weeks ago.以下内容适用于两周前我的 Win10 桌面的原始重新映像。 All patches were applied to system software before starting to deal with MySql.在开始处理MySql之前,所有补丁都被应用到系统软件上。

Just this morning I re-read the post in this thread (MySql Forums): https://forums.mysql.com/read.php?174,659102,660369#msg-660369就在今天早上,我重新阅读了这个帖子(MySql 论坛)中的帖子: https : //forums.mysql.com/read.php? 174,659102,660369#msg-660369

I thought that I had tried those suggestions before but I must have messed up one of the versions.我以为我之前已经尝试过这些建议,但我一定搞砸了其中一个版本。 I think that poster's basic strategy is sound.我认为海报的基本策略是合理的。 In my case I uninstalled all local MySql parts and the installer.就我而言,我卸载了所有本地 MySql 部件和安装程序。

I downloaded MySQL for Visual Studio - the Latest Development version (2.0.5 msi as of this writing) and installed that.我下载了用于 Visual Studio 的 MySQL -最新开发版本(在撰写本文时为 2.0.5 msi)并安装了它。

I found the OLD versions of the .Net Connector and installed 6.9.11我找到了 .Net 连接器的旧版本并安装了 6.9.11

In VS 2017 I created a dummy project using .Net 4.6.1.在 VS 2017 中,我使用 .Net 4.6.1 创建了一个虚拟项目。

I applied pending VS updates.我应用了挂起的 VS 更新。

I built the project.我建立了这个项目。

I went to "Project -> Manage Nuget Packages" and installed / downgraded EntityFramework to 6.0.我转到“项目 - > 管理 Nuget 包”并将 EntityFramework 安装/降级到 6.0。

I installed MySql.Data, MySql.Data.Entity and MySql.Web all at version 6.9.11我在 6.9.11 版本中安装了 MySql.Data、MySql.Data.Entity 和 MySql.Web

I cleaned and built the project then tried to add a new "Code First" model from a MySql DB... IT WORKED!我清理并构建了该项目,然后尝试从 MySql 数据库中添加一个新的“代码优先”模型......它奏效了!

Thank you 7 Reeds for coming back with an answer.谢谢 7 Reeds 回来回答。 It led me to get it working, although I did it slightly differently.它让我开始工作,尽管我的做法略有不同。

In VS 2017 to connect to MySQL 5.6:在 VS 2017 中连接 MySQL 5.6:

  1. Uninstall any MySQL products from the machine从机器上卸载任何 MySQL 产品
  2. Set Target framework to .NET Framework 4.5.2将目标框架设置为 .NET Framework 4.5.2
  3. Uninstall EntityFramework and any MySQL packages in Nuget在 Nuget 中卸载 EntityFramework 和任何 MySQL 包
  4. Install MySql.Data.Entity (6.10.7)安装 MySql.Data.Entity (6.10.7)

    It will also install the needed versions of the Nuget packages MySql.Data (6.10.7), EntityFramework (6.1.3), and BouncyCastle (1.8.3.1).它还将安装所需版本的 Nuget 包 MySql.Data (6.10.7)、EntityFramework (6.1.3) 和 BouncyCastle (1.8.3.1)。

    The MySql.Data.Entity package says it depends of Google.Protobuf, but it didn't install it and it works fine for me. MySql.Data.Entity 包说它取决于 Google.Protobuf,但它没有安装它,它对我来说很好用。

    It added References in my project to BouncyCastle.Crypto (1.8.3.0), EntityFramework (6.0.0.0), MySQL.Data (6.10.7.0), and MySql.Data.Entity.EF6 (6.10.7.0).它在我的项目中添加了对 BouncyCastle.Crypto (1.8.3.0)、EntityFramework (6.0.0.0)、MySQL.Data (6.10.7.0) 和 MySql.Data.Entity.EF6 (6.10.7.0) 的引用。

  5. Close Visual studio关闭视觉工作室

  6. Install 'MySQL for Visual Studio' (1.2.8)安装“MySQL for Visual Studio”(1.2.8)
  7. Install 'Connector/NET' (6.10.7)安装“连接器/NET”(6.10.7)
  8. Delete the bin and obj folders删除 bin 和 obj 文件夹
  9. Open Visual Studio and Rebuild Solution打开 Visual Studio 并重建解决方案
  10. Add New Item > ADO.NET Entity Data Model > Now you're in the wizard Add New Item > ADO.NET Entity Data Model > 现在您在向导中
  11. Select EF Designer from database EF Designer from database选择EF Designer from database
  12. On the next screen ('Entity Data Model Wizard'), click New Connection...在下一个屏幕(“实体数据模型向导”)上,单击New Connection...
  13. On the 'Connection Properties' screen click the Change... button for the 'Data source'在“连接属性”屏幕上,单击“数据源”的“ Change...按钮
  14. You should be able to select the MySQL Database as the 'Data source'您应该能够选择MySQL Database作为“数据源”
  15. The 'Data provider' should auto-populate with .NET Framework Data Provider for MySQL “数据提供者”应使用.NET Framework Data Provider for MySQL自动填充
  16. Yay!耶! Continue on like you normally would像往常一样继续

For posterity, here's the app.config for my WPF application:对于后代,这是我的 WPF 应用程序的 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <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, Version=6.10.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </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.10.7.0" newVersion="6.10.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="MyDB" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=my.example.com;user id=NotMyRealUserID;password=NotMyRealPassword;persistsecurityinfo=True;database=MyDatabase&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

I'm not sure what created this, but I have a 'User variable' in my 'Environment Variables' named MYSQLCONNECTOR_ASSEMBLIESPATH with value C:\\Program Files (x86)\\MySQL\\MySQL Connector Net 6.10.7\\Assemblies\\v4.5.2我不确定是什么造成了这个,但我的“环境变量”中有一个名为MYSQLCONNECTOR_ASSEMBLIESPATH的“用户变量”,其值为C:\\Program Files (x86)\\MySQL\\MySQL Connector Net 6.10.7\\Assemblies\\v4.5.2

At appsettings.json:在 appsettings.json:

"ConnectionStrings": { "DefaultConnection": "Server=localhost;database=MusicStoreDB;user id=root;Charset=utf8;" },

At ConfigureServices() in Startup.cs :在 Startup.cs 中的 ConfigureServices() 中:

using Microsoft.EntityFrameworkCore; using MySql.Data.MySqlClient; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection;

... services.AddDbContext(options => options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

Note: Change to: UseMysql <-------------- When using Pompelo EntityFrameworkCore注意:更改为:UseMysql <-------------- 使用 Pompelo EntityFrameworkCore 时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM