ASP.NET MVC使用MySQL数据库

时间:2021-12-04 07:17:28

1.新建MVC Web项目;
2.打开管理Nuget程序包 搜索:MySql.Data && MySql.Data.Entity 点击安装
3.修改连接字符串(默认是SqlServer)

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Gary.Web-20170523110735.mdf;Initial Catalog=aspnet-Gary.Web-20170523110735;Integrated Security=True" providerName="System.Data.SqlClient" />
<!--<add name="GaryDBContext" connectionString="Data Source=127.0.0.1;Initial Catalog=GaryWeb;Integrated Security=True;User Id=sa;Password=123456"
providerName="System.Data.SqlClient" />-->

<add name="GaryDBContext" connectionString="Data Source=127.0.0.1;Database=GaryWeb;User Id=root;Password=123456;charset=utf8;port=3306"
providerName="MySql.Data.MySqlClient" />

4.修改providers

<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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>