EF Code First Migrations数据库迁移
1、EF Code First创建数据库
步骤1:新建控制台应用程序
步骤2:安装EntityFramework
在程序包管理器控制台中执行以下语句:
PM>Install-Package EntityFramework
2、项目结构
两个实体及映射,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()); } } }
其他代码,可以从本博文最后下载。
3、EF Code First数据库迁移
步骤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
执行结果如下:
到此,数据完成了创建:
4、EF Code First添加实体
步骤1:增加City.cs实体
步骤2:在程序包管理器控制台执行以下语句:
PM>Add-Migration AddCity
执行结果如下:
步骤3:更新实体到数据库,在程序包管理器控制台执行以下语句
PM>Update-Database –Verbose
数据库变化如下:
5、EF Code First修改实体
步骤1:修改City,增加CityNo属性
步骤2:在程序包管理器控制台中执行以下代码:
PM>Add-Migration UpdateCity
执行结果如下:
步骤3:在程序包管理器控制台执行以下语句
PM>Update-Database –Verbose
执行结果如下:
6、版本回溯
步骤1:修改数据库中的City,删除CityNo字段
步骤2:在程序包管理器控制器执行以下语句:
PM>Add-Migration ModifyCity
步骤3:更新到数据库,在程序包管理器控制台执行以下语句:
PM>Update-Database –Verbose
在程序包管理器控制台执行以下语句:
PM>Update-Database –SourceMigration “201701091349455_AddCity.cs”
执行结果如下:
7、其他命令
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