public override void RegisterMigrations(DbMigrationSet migrations)
            {
                //ChildEntityRenamed has added reference column OtherParent (not null); we have to add a script that initializes the column for existing records,
                // otherwise adding foreign key constraint would fail. We set it to the same reference as Parent_Id column.
                // The script timing is 'Middle' - it will be executed AFTER column is added (as nullable), but before it is switched to NOT NULL and ref constraint is added.
                // SQLite does not allow renaming tables
                string tableName = migrations.ServerType == DbServerType.Sqlite ?
                                   "upd_ChildEntity" : migrations.GetFullTableName <IChildEntityRenamed>();
                var sql = string.Format(
                    @"UPDATE {0} SET ""OtherParent_Id"" = ""Parent_Id"" WHERE ""OtherParent_Id"" IS NULL;", tableName);

                migrations.AddSql("1.1.0.0", "InitOtherParentColumn", "Initialize values of IChildEntityRenamed.OtherParent column.",
                                  sql, timing: DbMigrationTiming.Middlle);

                // Let's add a post-upgrade action - it is a code that will be executed after upgrade is done and entity session is available
                // We add a couple of records to a new table.
                migrations.AddPostUpgradeAction("1.1.0.0", "InitNewTable", "Initializes NewTable", session => {
                    var ent1  = session.NewEntity <INewTable>();
                    ent1.Name = "Name1";
                    var ent2  = session.NewEntity <INewTable>();
                    ent2.Name = "Name2";
                    session.SaveChanges(); // this is optional, SaveChanges will be called after all actions are executed.
                });
            } //method
Exemple #2
0
            public override void RegisterMigrations(DbMigrationSet migrations)
            {
                //ChildEntityRenamed has added reference column OtherParent (not null); we have to add a script that initializes the column for existing records,
                // otherwise adding foreign key constraint would fail. We set it to the same reference as Parent_Id column.
                // The script timing is 'Middle' - it will be executed AFTER column is added (as nullable), but before it is switched to NOT NULL and ref constraint is added.
                // SQLite does not allow renaming tables
                string tableName = migrations.ServerType == DbServerType.Sqlite ? "ChildEntity" : migrations.GetFullTableName<IChildEntityRenamed>();
                var sql = string.Format(@"UPDATE {0} SET ""OtherParent_Id"" = ""Parent_Id"" WHERE ""OtherParent_Id"" IS NULL;", tableName);
                migrations.AddSql("1.1.0.0", "InitOtherParentColumn", "Initialize values of IChildEntityRenamed.OtherParent column.", sql, timing: DbMigrationTiming.Middlle);

                // Let's add a post-upgrade action - it is a code that will be executed after upgrade is done and entity session is available
                // We add a couple of records to a new table.
                migrations.AddPostUpgradeAction("1.1.0.0", "InitNewTable", "Initializes NewTable", session => {
                  var ent1 = session.NewEntity<INewTable>();
                  ent1.Name = "Name1";
                  var ent2 = session.NewEntity<INewTable>();
                  ent2.Name = "Name2";
                  session.SaveChanges(); // this is optional, SaveChanges will be called after all actions are executed.
                });
            }