Entity Framework Core
Links
- Entity Framework Code First Migrations in Team Environments 1
- Entity Framework Core Tutorial 2
- ms docs ef core 3
- Using Azure database for PostgreSQL in ASP.NET Core 4 (with EF Core) Jan18
- EF Core or micro-ORM? 5
- Entity Framework Core 2.1 Roadmap 6
- explicit loading 7
- implement infrastructure persistence layer with Entity Framework Core 8
Recipes
-
use an in memory db for tests
public class MessagingDbContext : Microsoft.EntityFrameworkCore.DbContext { /* ... */ } return new MessagingDbContext(new DbContextOptionsBuilder<MessagingDbContext>().UseInMemoryDatabase(databaseName: DB_NAME).Options) -
Fix
$ dotnet ef database updatereturning No executable found matching command “dotnet-ef”. Modify csproj.<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1"/> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet"> <Version>1.0.0-*</Version> </DotNetCliToolReference> -
Generate migration from Powershell / Git Bash
install-package entityframework.commands -pre dotnet ef migrations add "Initial" -o "Data\Migrations" dotnet ef database update
Data Seeding
Custom initialization logic with
UseSeedingandUseAsyncSeeding
Data Seeding 9
The New Way to Seed Your Database in EF Core 9 10
How to Seed Data with EF Core 9 and .NET Aspire 11
UseAsyncSeeding and UseSeeding are called as part of EnsureCreated, Migrate, and dotnet ef database update.
OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)=>
optionsBuilder
.UseSqlServer("YourConnectionString")
.UseAsyncSeeding(async (context, _, cancellationToken) =>
{
if (context.Set<T>().Any()) return;
SeedT();
await context.SaveChangesAsync(cancellationToken);
});
HasData
Model-managed data
HasData method 12 for seeding via OnModelCreating 13
Extension method pattern 15
Taking EF Core data seeding to the next level with Bogus 16
Migrations
Create and Drop APIs 17 — EnsureCreated/EnsureDeleted alternative to migrations
EF Core tools reference — .NET CLI 18
Migrations Overview 19
migrationBuilder.InsertData for manual migration customization
Compiled Models
Compiled models 20 — dotnet ef dbcontext optimize for startup performance