看了很多文章,嘗試了很多次總是進行不下去,整理一下,以便日後檢視。
1、建立ASP.NET MVC項目(EFCodeFirst)
1.1、右鍵點選引用選擇管理NuGet程式包下載下傳MySql.Data.Entity.dll(有依懶項,會自動下載下傳MySql.Data.dl 和 EntityFramework.dll)
1.2、修改Web.config檔案(如下圖)
2、建立類庫(EFCodeFirst.DataAccess)
2.1、右鍵添加-->建立項-->選擇資料-->選擇ADO.NET實體資料模型-->空Code First模型(也可以不用這一步,但DatabaseAccess.cs檔案需要自己建立)
2.1、右鍵點選引用選擇管理NuGet程式包下載下傳MySql.Data.Entity.dll(有依懶項,會自動下載下傳MySql.Data.dl 如下圖)
2.2、修改檔案 DatabaseAccess.cs
修改之前:
修改之後:
代碼如下:
namespaceEFCodeFirst.DataAccess
{usingIdentityAuth.Common;usingIdentityAuth.Model.Sys;usingMySql.Data.MySqlClient;usingSystem;usingSystem.Data.Common;usingSystem.Data.Entity;usingSystem.Linq;public classDatabaseAccess : DbContext
{#region 構造函數和初始化
private static string connectionStr { get; set; }private staticDbConnection GetConnection()
{
if(String.IsNullOrEmpty(connectionStr))
connectionStr= ConfigHelper.GetConnectionStr("EFCodeFirst");
if(String.IsNullOrEmpty(connectionStr))throw new Exception("資料庫連接配接字元串為空,請在Config檔案裡配置");
DbConnection dbConnection= new MySqlConnection() { ConnectionString =connectionStr };returndbConnection;
}public DatabaseAccess() : base(DatabaseAccess.GetConnection(), true)
{
Database.SetInitializer(null);
}#endregion
#region 系統表
///
///系統使用者資訊///
public DbSet UserEntities { get; set; }///
///角色資訊///
public DbSet RoleEntities { get; set; }///
///系統菜單資訊///
public DbSet MenuEntities { get; set; }///
///角色權限資訊///
public DbSet RoleAuthEntities { get; set; }///
///角色使用者資訊///
public DbSet RoleUserEntities { get; set; }#endregion}
}
2.3、修改App.config檔案(如下圖)
App.config檔案如下:
3、遷移
3.1、打開程式包管理控制器,預設項目選擇 EFCodeFirst.DataAccess(也就是CodeFirst遷移目錄,如下圖)
3.2、添加遷移配置 Enable-Migrations(生成以下檔案并會出現錯誤,如圖)
3.3、解決3.2出現的錯誤,修改生成檔案 Configuration.cs(如下圖)
代碼如下:
namespaceEFCodeFirst.DataAccess.Migrations
{usingSystem;usingSystem.Data.Common;usingSystem.Data.Entity;usingSystem.Data.Entity.Migrations;usingSystem.Data.Entity.Migrations.History;usingSystem.Linq;internal sealed class Configuration : DbMigrationsConfiguration{publicConfiguration()
{
AutomaticMigrationsEnabled= false;
SetSqlGenerator("MySql.Data.MySqlClient", newMySql.Data.Entity.MySqlMigrationSqlGenerator());
SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) => newMySqlHistoryContext(conn, schema));
}protected override voidSeed(EFCodeFirst.DataAccess.DatabaseAccess context)
{//This method will be called after migrating to the latest version.//You can use the DbSet.AddOrUpdate() helper extension method//to avoid creating duplicate seed data.
}
}public classMySqlHistoryContext : HistoryContext
{public MySqlHistoryContext(DbConnection connection, stringdefaultSchema)
:base(connection, defaultSchema)
{}protected override voidOnModelCreating(DbModelBuilder modelBuilder)
{base.OnModelCreating(modelBuilder);
modelBuilder.Entity().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
modelBuilder.Entity().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();
}
}
}
3.4、添加一個合并遷移指令行:Add-Migration [Name](eg:Add-Migration Add_Base_Table) Name 為一個合并的名稱,自定義,最好不重複
3.5、生成SQL:Update-Database -Script(如果不需要SQL可不用執行)
3.6、遷移到資料庫:Update-Database