Skip to content

Instantly share code, notes, and snippets.

@colltoaction
Last active March 3, 2023 13:48
Show Gist options
  • Save colltoaction/673a1aa7feb542dbe3363020eb105527 to your computer and use it in GitHub Desktop.
Save colltoaction/673a1aa7feb542dbe3363020eb105527 to your computer and use it in GitHub Desktop.
EF Core data motion proposal. These three examples are sequential: first we have three seed data values, then four, then we update one of them.
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 },
});
});
}
/* countries.json file
[
{ "name": "Germany", "population": 82175700 },
{ "name": "France", "population": 65447374 },
{ "name": "Belgium", "population": 10839905 },
{ "name": "Netherlands", "population": 16680000 }
]
*/
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(entity =>
{
var jsonCountries = File.ReadAllText(@"countries.json");
var countries = JsonConvert.DeserializeObject<Country[]>(jsonCountries);
entity.SeedData(countries);
});
}

System data

I want to be able to seed static system data, for example a list of countries or states

Migrations

I want to be able to manipulate data as part of a specific migration, adding it on Up and deleting it on Down

Raw SQL

I want to be able to run raw SQL statements as part of my migration data transformations

Multiple instances

I want to be able to seed on startup even if I have multiple servers spinning up at the same time

Publishing

I want to be able to run data transformations as part of my publishing process

Simple deployment

I want to be able to run migrations as part of my application startup

Multiple environments

I want to be able to run seed methods with specific environment configurations

Static typing

I want to be able to write static typed data motion code

Automatic seeding

I want to be able to set up seeding methods that run automatically when updating my database

Programmatic API

I want to be able to run migrations from code, for example as part of my InMemory database set up

File

I want to be able to have a file (e.g. JSON) with my seed data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment