- description
- prepare database context
- managing migrations
- run the app
- migration tool summary
- clean architecture
- how this project was built
👉 one-to-one, one-to-many, many-to-many code first relationship examples here
Shows how to create a code-first local db using sqlite but the same approach can applied using other backends.
- add nuget pkg of EntityFrameworkCore.Design and for specific backend used
- create table as code first
- create db context
- override OnConfiguring where setup specific backend used
- override OnModelCreating where table fields can be configured ( primary keys, indexes, ... ) or seeding of initial data can be added
- create a dataset foreach of the table wants to be materialized on the db
Migrations in code-first db allow team of developers to work on the same db project each with their on local db and versioning their changes through commits that includes Migrations
folder.
The first-est migration is done by a developer and acts as an entry point for the work on that database so other developers can add their code-first changes after that commit by adding further migrations including code and Migrations
folder changes.
Because each migration has a timestamp in the filename there aren't conflict on these while the Migrations/LocalDbContextModelSnapshots.cs
can be subjected to normal git conflicts in some circumnstances that can be resolved in the usual way. To reduce conflict situation, regular pulls could help.
Install dotnet ef migrations tools:
dotnet tool install --global dotnet-ef
Create initial migration:
cd skeleton-netcore-ef-core-first
dotnet ef migrations add initial
Commit the initial migration ( I didn't added the initial migration for didactic purpose ).
Create a migration doesn't imply the code-first materialize on database, in order to do that, issue:
dotnet ef database update
This command can be used also to revert a migration applied by specifying a name of a migration already applied and all migrations after that one specified will be reverted returning to a state of "pending".
Migrations can be removed, when are in pending state, one by one from tail using dotnet ef migrations remove
.
dn run
Started with 0 records
add new one [newRecord1]
changes: SampleData {Id: -9223372036854774807} Added
cmd | description |
---|---|
dotnet ef migrations list |
list migrations showing which aren't yet applied (pending) |
dotnet ef database update |
apply pending migrations |
dotnet ef migrations add MIGRATION_NAME |
add migrations |
dotnet ef migrations remove |
remove latest not yet committed migration |
dotnet ef databse update MIGRATION_TO |
revert applied migration |
To improve maintainability, modularity and separation of concerns in enterprise applications the Clean architecture should evaluated; there are many boiler plate templates available:
dotnet new search clean
dotnet new console -n skeleton-netcore-ef-code-first -f net7.0 --langVersion 11
cd skeleton-netcore-ef-code-first
dotnet add package Microsoft.EntityFrameworkCore.Design --version 7.0.5
dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 7.0.5