MigrateDatabaseToLatestVersion
.enable-migrations –EnableAutomaticMigration:$true
internal sealed class Configuration : DbMigrationsConfiguration<SchoolDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(SchoolDataLayer.SchoolDBContext context)
{
// This method will be called after migrating to the latest version.
// Use the DbSet<T>.AddOrUpdate() method to avoid creating duplicate seed data.
context.People.AddOrUpdate(
p => p.FullName,
new Person { FullName = "Andrew Peters" },
new Person { FullName = "Brice Lambson" },
new Person { FullName = "Rowan Miller" }
);
}
}
MigrateDatabaseToLatestVersion
.public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base("SchoolDBConnectionString")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext,
SchoolMigrationsConfiguration>("SchoolDBConnectionString"));
}
}
__MigrationHistory
along with other tables.AutomaticMigrationDataLossAllowed = true
in the configuration class constructor, along with AutomaticMigrationsEnabled = true
.Add-migration: It will scaffold the next migration for the changes you have made to your domain classes.
Add-Migration [-Name] <String> [-Force]
[-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
[-IgnoreChanges] [<CommonParameters>]
Add-Migration [-Name] <String> [-Force]
[-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] -ConnectionString <String>
-ConnectionProviderName <String> [-IgnoreChanges] [<Common Parameters>]
Update-database: It will apply pending changes to the database based on latest scaffolding code file you create using "Add-Migration" command.
Update-Database [-SourceMigration <String>]
[-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
[-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
[-ConnectionStringName <String>] [<CommonParameters>]
Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] -ConnectionString <String>
-ConnectionProviderName <String> [<CommonParameters>]
Update-database
command with -TargetMigration
parameter to roll back the database schema to any of the previous states.get-migration
" command to see what migration has been applied.