Skip to content

Instantly share code, notes, and snippets.

@colltoaction
Created February 22, 2017 00:32
Show Gist options
  • Save colltoaction/6cdb49ad81cffd6affab22d05db380f3 to your computer and use it in GitHub Desktop.
Save colltoaction/6cdb49ad81cffd6affab22d05db380f3 to your computer and use it in GitHub Desktop.
EF Core data motion proposal
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Countries",
columns: table => new
{
Name = table.Column<string>(nullable: false),
Population = table.Column<int>(nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_Country", x => x.Name);
}
).WithRows(new [] { // Note: we could get property names from the column names
new { Name = "Germany", Population: 81831000 },
new { Name = "France", Population: 65447374 },
new { Name = "Belgium", Population: 10839905 },
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Countries");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(entity =>
{
entity.SeedData(new Country[]
{
new Country { Name = "Germany", Population: 81831000 },
new Country { Name = "France", Population: 65447374 },
new Country { Name = "Belgium", Population: 10839905 },
});
});
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.InsertRows(
table: "Countries",
rows: new [] { // Note: we don't get compiler suggestions here
new { Name = "Netherlands", Population: 16680000 },
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DeleteRows(
table: "Countries",
rows: new [] { "Netherlands" }); // Note: this is an array of the primary keys
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(entity =>
{
entity.SeedData(new Country[]
{
new Country { Name = "Germany", Population: 81831000 },
new Country { Name = "France", Population: 65447374 },
new Country { Name = "Belgium", Population: 10839905 },
new Country { Name = "Netherlands", Population: 16680000 }, // Adding new entity
});
});
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateRows(
table: "Countries",
rows: new [] { // Note: we automatically detect the primary key
new { Name = "Germany", Population: 82175700 },
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateRows(
table: "Countries",
rows: new [] { // Note: we automatically detect the primary key
new { Name = "Germany", Population: 81831000 },
});
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(entity =>
{
entity.SeedData(new Country[]
{
new Country { Name = "Germany", Population: 82175700 }, // Updating population value
new Country { Name = "France", Population: 65447374 },
new Country { Name = "Belgium", Population: 10839905 },
new Country { Name = "Netherlands", Population: 16680000 },
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment