1. 程式人生 > >EF+mvc+mysql

EF+mvc+mysql

ngs 獲得 .net rsh unique .config nes mode schema

這個真是一個大坑啊.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添加到引用中。

EF+mvc+mysql