controlling-database-creation-and-schema-with-migrations-slides
controlling-database-creation-and-schema-with-migrations-slides
Julie Lerman
EF Core Expert and Software Coach
@julielerman | thedatafarm.com
Module Overview of EF Core Migrations API
Overview Setting up your project and Visual Studio
for migrations
Create and inspect a migration file
Using EF Core Migrations to create a
database or database scripts
Reverse engineer an existing database into
classes and DbContext
Understanding EF Core Migrations
EF Core Needs to Comprehend the DB Schema
+
DbContext Conventional and Database schema
custom mappings
Mapping knowledge can
also be used to evolve the
database schema.
EF Core Basic Migrations Workflow
Command logic
Migrations APIs
Migration Commands Lead to Migrations APIs
Command logic
Tools depends on Design Microsoft.EntityFrameworkCore.Design
Which Project
Gets the Tools?
Getting the Package Manager Console
Ready to Use Migrations
Using Migrations in Visual Studio
When EF Core Is in a Class Library Project
Install Microsoft.EntityFrameworkCore.Tools package into
executable project (e.g., console)
Add EF Use
Add EF Core tools Core Design** “dotnet ef”
to system* package to commands at
executable project command line
Read DbContext to
determine data model
Migration File
Applying Migrations
Migration File
Update-Database
Script-Migration
Applying Migrations Directly to the Database
Migration File
Update-Database
Migration File
• Reads migration file
• Generates SQL
• Default: Displays SQL in editor
• Use parameters to target file name etc.
Script-Migration
update-database script-migration
API’s internal code will create the database You must create the database before
before executing migration code running the script
Seeding a Database via Migrations
modelBuilder.Entity<EntityType>().HasData(parameters)
Read DbContext to
determine data model
...and any seeding code
script-migration script-migration
script-migration
-idempotent FROM TO
Tables Views
Reverse Engineer with the Scaffold Command
EF Core CLI
PowerShell
dotnet ef dbcontext
Scaffold-DbContext
scaffold
Scaffolding Limitations
VS extension: ErikEJ.EFCorePowerTools
Free
Open-source
(github.com/ErikEJ/EFCorePowerTools)
Built and maintained by Erik Ejlskov Jensen
Many more features besides reverse engineer
How EF Core Determines Mappings to DB