NET 6 - Setup Entity Framework

Last modified: June 21, 2021
Create Database

sql sql sql

Repeat it for MVC, Razor Page and WebApi
Configure appsettings.json

Since we are database running on our computer (i.e. localhost) and our database name is Event, hence we set up as below

"DefaultConnection": "Server=localhost;Database=Event;Trusted_Connection=True;MultipleActiveResultSets=true"

sql

Configure startup.cs
builder.Services.AddDbContext<TicketingDbContext>(options => options.UseSqlServer(connectionString));

sql

Migrations

Create migration file for EventContext

Add-Migration InitialCreate -Context EventContext -OutputDir Migrations\SqlServerMigrations

Peform migration to create table for identity models (i.e. ApplicationDbContext)

Update-Database -Context ApplicationDbContext

Peform migration to create table for Event and EventBooking models

Update-Database -Context TicketingDbContext