该实例实现了:
- 添加列
- 定制化迁移
- 数据移动/定制sql
- 在应用程序启动时自动升级(MigrateDatabaseToLatestVersion初始化器)
只需要配置下app.config中的数据库连接串,即可自动生成数据库以及表结构,具体如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.Entity;using MigrationsDemo.Migrations; namespace MigrationsDemo{ class Program { static void Main(string[] args) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>()); using (var db = new BlogContext()) { db.Blogs.Add(new Blog { Name = "Another Blog" }); db.SaveChanges(); foreach (var blog in db.Blogs) { Console.WriteLine(blog.Name); } } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }}
评论