简体   繁体   English

MySql和Visual Studio 2013集成

[英]MySql and Visual Studio 2013 Integration

Though I have been developing with Sql Server for a log time, the primary reason I wanted to move to MySql was because of some client requirement. 尽管我一直在使用Sql Server进行开发,但我想迁移到MySql的主要原因是由于某些客户端需求。 So all the developments I have done on the front end & business logic remained the same and I just wanted to flip the database to MySql. 因此,我在前端和业务逻辑上所做的所有开发都保持不变,我只想将数据库翻转到MySql。

I had lots of problems migrating to MySql with Visual Studio especially 2013. And I spent few days figuring out how to use the extensions and Entity Model generations in this. 我在使用Visual Studio(尤其是2013)迁移到MySql时遇到很多问题。花了几天的时间弄清楚如何在其中使用扩展和实体模型。 Though this is pretty much specific to VS 2013, this can also be easily applied to 2012. 尽管这几乎是VS 2013特有的,但也可以轻松应用于2012年。

The first and foremost thing that every developer would love to have is to be able to select the MySql driver from your Visual Studio while you are either generating Model classes from the MySql database or trying to manage the database from your IDE as found in the image below. 每个开发人员最想拥有的第一件事就是在您从MySql数据库生成Model类或尝试从IDE中管理数据库的过程中,从Visual Studio中选择MySql驱动程序,如镜像所示。下面。

Visual Studio中的MySql驱动程序

But this was a slightly distant from a normal reach as it is a 2 step process. 但这距离正常范围稍有距离,因为它是一个两步过程。

  • If you do not have a MySql version installed, I heavily recommend the wamp installer which can be downloaded from http://www.wampserver.com/en/ . 如果您没有安装MySql版本,我强烈建议您从http://www.wampserver.com/en/下载wamp安装程序。 This installs mysql, apache services and can be started with a start menu short cut. 这将安装mysql和apache服务,并且可以使用开始菜单快捷方式启动。 Also it runs on port 80 by default. 默认情况下,它还在端口80上运行。 If you want to change the port, edit the C:\\wamp\\bin\\apache\\Apache_version\\conf\\httpd.conf file and set the port to Listen 如果要更改端口,请编辑C:\\ wamp \\ bin \\ apache \\ Apache_version \\ conf \\ httpd.conf文件并将端口设置为Listen
  • Install the MySql Connector for .Net with the latest version. 使用最新版本安装用于.Net的MySql Connector。 The time when I used it, it was version 6.8.3 and worked reasonably well. 当我使用它的时候,它是6.8.3版本,并且运行良好。 Download and install it from http://dev.mysql.com/downloads/connector/net/ link. http://dev.mysql.com/downloads/connector/net/链接下载并安装它。
  • The second step is to install MySql for Visual studio and the version I used is current http://dev.mysql.com/downloads/windows/visualstudio/ 第二步是安装MySql for Visual Studio,我使用的版本是当前的http://dev.mysql.com/downloads/windows/visualstudio/

    After doing this, you'll be able to see the MySql driver on your Model generator. 完成此操作后,您将能够在模型生成器上看到MySql驱动程序。

    • In your projects do an Install-Package Mysql.Data.Entities which automatically installs the latest mysql version in your projects. 在您的项目中执行Install-Package Mysql.Data.Entities,它会在您的项目中自动安装最新的mysql版本。
    • Replace the following web.config entries which are found as 替换以下找到的web.config条目:
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
to the following.
<entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
    </DbProviderFactories>
  </system.data>

Another bug I noticed is, when you - create a new Ado .net entity Model 我注意到的另一个错误是,当您-创建新的Ado .net实体模型时
- Try to generate the model classes directly, it says that the Entity Framework provider is very latest and it does not have the necessary drivers for generating mysql model. -尝试直接生成模型类,它表示Entity Framework提供程序是最新的,并且没有用于生成mysql模型的必要驱动程序。

If you encounter this error, - create an Ado .Net Entity model as an "Empty Model" - After that try to "Update Model From Database" with the connections & context names - If it gives the same error, rebuild/restart Visual Studio 2013 and the project. 如果遇到此错误,-将Ado .Net实体模型创建为“空模型”-之后,尝试使用连接和上下文名称“从数据库更新模型”-如果给出相同的错误,则重新构建/重新启动Visual Studio 2013年和该项目。 Trust me "It works this way". 相信我“它以这种方式工作”。

Another problem I faced while moving from Sql Server to Mysql is the lower case table names (default in mysql) which eventually generates models in lower case names. 从Sql Server迁移到Mysql时,我遇到的另一个问题是小写的表名(mysql中的默认表名),该表名最终会生成小写的模型。 To avoid this, you can set "lower_case_table_names=0" setting in C:\\wamp\\bin\\mysql\\mysql5.1.xx\\my.ini file in the end and restart wamp. 为避免这种情况,最后可以在C:\\ wamp \\ bin \\ mysql \\ mysql5.1.xx \\ my.ini文件中设置“ lower_case_table_names = 0”设置,然后重新启动wamp。 This creates tables in CamelCase and you can quickly move from Sql Server to MySql. 这将在CamelCase中创建表,您可以快速从Sql Server迁移到MySql。

Also while generating the model from database, sometimes the EF wizard might hang. 同样,从数据库生成模型时,有时EF向导可能会挂起。 To avoid this, if you open & retrieve data from a table inside Visual Studio, this problem generally goes away. 为避免这种情况,如果您从Visual Studio内部的表中打开并检索数据,则通常会解决此问题。

Hope this helps someone save their time. 希望这可以帮助某人节省时间。 If anyone find some new tweaks, please help us by sharing. 如果有人发现一些新的调整,请通过共享帮助我们。

I use Visual Studio 2012. Correct process of installing MySql will help you avoid any such error. 我使用Visual Studio2012。正确的MySql安装过程将帮助您避免任何此类错误。 At least this is what i did. 至少这是我所做的。

Download and install visual studio 2012 Download and install mysql-installer-community-5.6.17.0 Download and install mysql-connector-net-6.8.3 Download and install mysql-visualstudio-plugin-1.1.1 Order is important and each item too. 下载并安装Visual Studio 2012下载并安装mysql-installer-community-5.6.17.0下载并安装mysql-connector-net-6.8.3下载并安装mysql-visualstudio-plugin-1.1.1顺序很重要,每个项目也很重要。 Open Visual Studio and Create new project Add reference of MySql.Data in solution explorer after right clicking the project name View server explorer Add new connection and fill the spaces correctly At the top of your code, add using MySql.Data.MySqlClient 右键单击项目名称后,打开Visual Studio并创建新项目,在解决方案资源管理器中添加MySql.Data的引用。查看服务器资源管理器添加新连接并正确填充空格在代码的顶部,使用MySql.Data.MySqlClient添加

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

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