Code-first approach Data Approach with Migrations
The code-first approach is a software development methodology, particularly relevant in the context of Object-Relational Mapping (ORM) frameworks like Entity Framework, where you define your data model in code first, and the database schema is then generated from this code.
The ORM framework then takes these code model /definitions and creates the corresponding tables, columns, keys, and relationships in the database.
Migrations
Migration will create a database, inside a SQL Server with the name that we provided inside the the connection string. Once created, that database will be used and configured with our application.
NuGet Package for Migration
- Microsoft.EntityFrameworkCore.Tools
Run Migration Commands
- What these commands do: creating a script in terms of a C#. It is creating a script that entity framework core can use to create a later on a SQL script and then create a database.
- Add-Migration “Initial Project”
- it has created a new migration file and that resides in the Migrations folder that has been created by Entity Framework Core.
- Migration files has all the code that is needed to create our tables that we have. One is the blog posts table and the other one is the categories table and it has all the column information in it as well.
- update-database
- Entity framework core will read these mi migration files and have a connection with the SQL Server that we mentioned inside the app settings and if a database does not exist, it will create one.
- This also create tables mention in migration files
- Add-Migration “Initial Project”
DB context class always and always works with the domain model folders