这个真是一个大坑啊.TM折腾了一下午终于弄好了.赶紧记录下来分享给大家,免得有和我一样一直配置不成功的又折腾半天….
1.安装MySQL for Visual Studio
这个直接在mysql官网下载并安装就好了.
不过这个必须是vs2013 professional版本以上才可以!!
2.安装MySQL Connector/Net
这个可以可以通过NuGet工具获得,比较轻松愉快,当然你也可以自己下载,自己引用.
3.配置web.config.
首先是connectionStrings节点
1 <connectionStrings> 2 <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MyMVC-20150122222755;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MyMVC-20150122222755.mdf" providerName="System.Data.SqlClient" /> 3 <add name="MovieDBContext" providerName="MySql.Data.MySqlClient"connectionString="server=localhost; 4 port=3306;database=mymvc;uid=root;password="/> 5 </connectionStrings>
然后是system.data节点和system.web节点
system.web添加如下代码
1 <roleManager enabled="true" defaultProvider="MySQLRoleProvider"> 2 <providers> 3 <clear/> 4 <add name="MySQLRoleProvider" autogenerateschema="true" 5 type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.8.4.0, 6 Culture=neutral, PublicKeyToken=c5687fc88969c44d" 7 connectionStringName="MarioDB" applicationName="/" /> 8 </providers> 9 </roleManager> 10 11 <membership defaultProvider="MySQLMembershipProvider"> 12 <providers> 13 <clear /> 14 <add name="MySQLMembershipProvider" autogenerateschema="true" 15 type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, 16 Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" 17 connectionStringName="MarioDB"enablePasswordRetrieval="false" 18 enablePasswordReset="true"requiresQuestionAndAnswer="false" 19 requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" 20 minRequiredPasswordLength="6"minRequiredNonalphanumericCharacters="0" 21 passwordAttemptWindow="10" applicationName="/" /> 22 </providers> 23 </membership>
system.data节点
1 <system.data> 2 <DbProviderFactories> 3 <remove invariant="MySql.Data.MySqlClient"/> 4 <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" 5 description=".Net Framework Data Provider for MySQL" 6 type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" /> 7 </DbProviderFactories> 8 </system.data>
注意将上面的Version=6.8.4.0修改成你的版本号
4.在你的module类中实现DbContext的默认构造函数,如下面我的
1 using System; 2 using System.Data.Entity; 3 using MySql.Data; 4 using MySql.Data.Entity; 5 using MySql.Web.Security; 6 namespace MyMVC.Models 7 { 8 public class Movie 9 { 10 public int ID { get; set; } 11 public string Title { get; set; } 12 public DateTime ReleaseDate { get; set; } 13 public string Genre { get; set; } 14 public decimal Price { get; set; } 15 } 16 17 18 19 public class MovieDBContext : DbContext 20 { 21 public MovieDBContext() : base("MovieDB") { } 22 public DbSet<Models.Movie> Moves { get; set; } 23 } 24 }
5.将Mysql.Data和Mysql.Web添加到引用中。