Created
February 22, 2017 00:32
-
-
Save colltoaction/6cdb49ad81cffd6affab22d05db380f3 to your computer and use it in GitHub Desktop.
EF Core data motion proposal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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