ZKWebFramework数据库迁移流程


主框架(ZKWebFramework)是根据程序集中DbContext进行数据库迁移的。
例如YiMei.Main程序集:
DbContext名称为:YiMeiMigrationDbContext
YiMeiMigrationDbContext那类中有一个静态初始化函数{static YiMeiMigrationDbContext()}

函数中存有数据迁移的配置。
Database.SetInitializer(
            new MigrateDatabaseToLatestVersion<YiMeiMigrationDbContext, Configuration>());

在每一次数据迁移的过程中, Add-Migration 生成的文件不需要删除。
也不需要执行 Update-Database。因为主框架运行会反射各个程序集进行生成数据库。
//
流程1:如果存在Configuration.cs
当实体类(DataBase)有所修改的时候,
Add-Migration进行数据迁移。
然后编译,运行主框架即可。
//--------------------------------------------------------------------------------------
流程2:如果运行Add-Migration失败,那便把Configuration.cs移除,修改
DbContext.cs文件中静态初始化函数
Database.SetInitializer(
            new MigrateDatabaseToLatestVersion<YiMeiMigrationDbContext, Configuration>());

Database.SetInitializer<DbContext类名>(null);

然后运行:Enable-Migrations -EnableAutomaticMigrations命令,
之后进行Add-Migration,再把DbContext.cs文件中静态初始化函数改成
Database.SetInitializer(
            new MigrateDatabaseToLatestVersion<YiMeiMigrationDbContext, Configuration>());
编译后运行主框架即可。

 

生成的数据,一定要重新将之前存在的数据给删除掉

比如:Common_User Common_UserRole等

 

  CreateTable(
                "dbo.Common_User",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Username = c.String(nullable: false),
                        Nickname = c.String(nullable: false),
                        Password = c.String(nullable: false),
                        CreateTime = c.DateTime(nullable: false),
                        DeleteState = c.Int(nullable: false),
                        Role_Id = c.Long(),
                        Remark = c.String(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Common_UserRole", t => t.Role_Id)
                .Index(t => t.Username, unique: true, name: "INDEX_Username")
                .Index(t => t.Role_Id);
            
            CreateTable(
                "dbo.Common_UserRole",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        PrivilegesContent = c.String(nullable: false),
                        CreateTime = c.DateTime(nullable: false),
                        LastUpdated = c.DateTime(nullable: false),
                        Remark = c.String(),
                    })
                .PrimaryKey(t => t.Id);

 

 

            DropTable("dbo.Common_UserRole");
            DropTable("dbo.Common_User");

 

posted @ 2015-09-11 07:32  中酷科技ZKWeb博客  阅读(326)  评论(0编辑  收藏  举报