Instruction file imported from Raph8277/Hello_gRPC (
.github/instructions/efcore.instructions.md). Copyright stays with the author.
EF Core Guidelines
Architecture
- Entities, DbContext, migrations, and seeding live in
Hello_gRPC.Data(namespaceHelloGrpc.Data) - Business logic services live in
Hello_gRPC.Service(namespaceHelloGrpc.Service) - gRPC services in Backend delegate to the Service layer
Entity Conventions
- Use
requiredkeyword for non-nullable string properties - Use
DateOnlyfor dates (notDateTime) - Use
DateTimeonly forCreatedAt/UpdatedAttimestamps - Primary key:
Id(int, auto-increment) - Configure constraints via Fluent API in
OnModelCreating
DbContext
- Use primary constructor for
DbContextOptionsinjection - Override
SaveChangesAsyncto auto-update timestamps - Use
Set<T>()expression-bodied DbSet properties - SQLite connection string:
Data Source=hello_grpc.db
Migrations
dotnet ef migrations add <Name> --project src/Hello_gRPC.Data/ --startup-project src/Hello_gRPC.Backend/
dotnet ef database update --project src/Hello_gRPC.Data/ --startup-project src/Hello_gRPC.Backend/
Seeding
- Check
AnyAsync()before seeding to avoid duplicates - Call
Database.MigrateAsync()at startup before seeding - Use the
DatabaseSeeder.SeedAsync()static method inHelloGrpc.Data
Service Layer
{Entity}Servicewith primary constructor injectingAppDbContext- Async CRUD methods:
GetAllAsync,GetByIdAsync,AddAsync,UpdateAsync,DeleteAsync - DI registration via extension methods in
ServiceCollectionExtensions