Entity Framework Core part2 - dbcontext and migration
Вставка
- Опубліковано 16 гру 2024
- In this series
✅Getting started
ORM: Object relational mapping, map (convert 2 directions - from code C# to sql and from sql map to C# object)
Sql / Sql Server: [Optional] but good to know before starting with EF
Setup / Installation
Install package: learn.microsof...
```bash
dotnet new webapi -n MusicApi # MusicApi.csproj
dotnet add package MusicApi.csproj Microsoft.EntityFrameworkCore.SqlServer
dotnet add -h
to find csproj file
git ls-files | grep .csproj$
find . | grep .csproj$
```
✅DbContext & entities
Setup DbContext
Setup entities: POCO
Register dbContext to service collection
Configure connection string: could be in appsettings.json or user-secrets…
✅Apply migration
Install necessary tools & packages
```bash
first, install dotnet ef tool
dotnet tool install dotnet-ef [--create-manifest-if-needed] # install dotnet-ef @latest version
install Design package
dotnet add package MusicApi.csproj Microsoft.EntityFrameworkCore.Design # latest version
```
Create and apply migration
```bash
dotnet ef migrations add [Name] -s /path/to/startup.csproj -p /path/to/target.csproj -c DbContextName
dotnet ef migrations add InitDb -p MusicApi.csproj
dotnet ef database update -p MusicApi.csproj
```
CRUD
Entities
Relationship
Configuration
Seeding
Migration
Working with data
Query
Tracking
Update
Bulk operators
Saving
More
Logging
Tips & tricks
Patterns (Repository / Unit of work)
Practices
Interceptor