步驟1:新建控制台應用程序
步驟2:安裝EntityFramework
在程序包管理器控制台中執行以下語句:
PM>Install-Package EntityFramework
兩個實體及映射,PortalContext代碼如下:
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Enities;
using Portal.Mapping;
namespace Portal
{
public class PortalContext : DbContext
{
static PortalContext()
{
System.Data.Entity.Database.SetInitializer<PortalContext>(null);
//Database.SetInitializer(new DropCreateDatabaseAlways<PortalContext>());
}
public DbSet<Category> Categories { get; set; }
public DbSet<Province> Provinces { get; set; }
/// <summary>
/// This method is called when the model for a derived context has been initialized, but
/// before the model has been locked down and used to initialize the context. The default
/// implementation of this method does nothing, but it can be overridden in a derived class
/// such that the model can be further configured before it is locked down.
/// </summary>
/// <remarks>
/// Typically, this method is called only once when the first instance of a derived context
/// is created. The model for that context is then cached and is for all further instances of
/// the context in the app domain. This caching can be disabled by setting the ModelCaching
/// property on the given ModelBuidler, but note that this can seriously degrade performance.
/// More control over caching is provided through use of the DbModelBuilder and DbContextFactory
/// classes directly.
/// </remarks>
/// <param name="modelBuilder"> The builder that defines the model for the context being created. </param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
modelBuilder.Configurations.Add(new ProvinceMap());
}
}
}
其他代碼,可以從本博文最後下載。
步驟1:生成數據庫
修改PortailContext.cs的靜態構造函數,取消當數據庫模型發生改變時刪除當前數據庫重建數據庫的設置。
static PortalContext()
{
System.Data.Entity.Database.SetInitializer<PortalContext>(null);
//Database.SetInitializer(new DropCreateDatabaseAlways<PortalContext>());
}
步驟2:在程序包管理器控制台,執行以下語句
PM>Enable-Migrations –EnableAutomaticMigrations
運行結果如下:
Configuration代碼如下
namespace Portal.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Portal.PortalContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Portal.PortalContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
步驟3:在程序包管理器控制台執行以下語句
PM>Add-Migration InitialCreate
運行結果如下:
步驟4:在程序包管理器控制台執行以下語句
PM>Update-Database –Verbose
執行結果如下:
到此,數據完成了創建:
步驟1:增加City.cs實體
步驟2:在程序包管理器控制台執行以下語句:
PM>Add-Migration AddCity
執行結果如下:
步驟3:更新實體到數據庫,在程序包管理器控制台執行以下語句
PM>Update-Database –Verbose
數據庫變化如下:
步驟1:修改City,增加CityNo屬性
步驟2:在程序包管理器控制台中執行以下代碼:
PM>Add-Migration UpdateCity
執行結果如下:
步驟3:在程序包管理器控制台執行以下語句
PM>Update-Database –Verbose
執行結果如下:
步驟1:修改數據庫中的City,刪除CityNo字段
步驟2:在程序包管理器控制器執行以下語句:
PM>Add-Migration ModifyCity
步驟3:更新到數據庫,在程序包管理器控制台執行以下語句:
PM>Update-Database –Verbose
在程序包管理器控制台執行以下語句:
PM>Update-Database –SourceMigration “201701091349455_AddCity.cs”
執行結果如下:
1、為指定的DbContext啟用數據庫遷移
PM> Enable-Migrations -ContextTypeName Portal.PortalContext
2、設置是否允許自動遷移
Enable-Migrations
生成的Configuration.cs類文件的構造函數
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
3、Enable-Migrations指定項目名稱
PM>Enable-Migrations -StartUpProjectName Portal
如果在“Package Manager Console”中選擇了默認項目可以不設置“-StartUpProjectName”參數;如果多次執行此命令可以添加-Force參數。
4、查看所執行的Sql語句 -Verbose指令
Update-Database -Verbose
Portal示例下載:http://files.cnblogs.com/files/zsy/Portal.rar