Entity Framework Core

Entity Framework Core

Lists Entity Framework Core best practices for DbContext design, entities, performance, migrations, querying and change tracking.

Skill files1 file
SKILL.md76 lines
1
---
2
name: ef-core
3
description: 'Get best practices for Entity Framework Core'
4
---
5
6
# Entity Framework Core Best Practices
7
8
Your goal is to help me follow best practices when working with Entity Framework Core.
9
10
## Data Context Design
11
12
- Keep DbContext classes focused and cohesive
13
- Use constructor injection for configuration options
14
- Override OnModelCreating for fluent API configuration
15
- Separate entity configurations using IEntityTypeConfiguration
16
- Consider using DbContextFactory pattern for console apps or tests
17
18
## Entity Design
19
20
- Use meaningful primary keys (consider natural vs surrogate keys)
21
- Implement proper relationships (one-to-one, one-to-many, many-to-many)
22
- Use data annotations or fluent API for constraints and validations
23
- Implement appropriate navigational properties
24
- Consider using owned entity types for value objects
25
26
## Performance
27
28
- Use AsNoTracking() for read-only queries
29
- Implement pagination for large result sets with Skip() and Take()
30
- Use Include() to eager load related entities when needed
31
- Consider projection (Select) to retrieve only required fields
32
- Use compiled queries for frequently executed queries
33
- Avoid N+1 query problems by properly including related data
34
35
## Migrations
36
37
- Create small, focused migrations
38
- Name migrations descriptively
39
- Verify migration SQL scripts before applying to production
40
- Consider using migration bundles for deployment
41
- Add data seeding through migrations when appropriate
42
43
## Querying
44
45
- Use IQueryable judiciously and understand when queries execute
46
- Prefer strongly-typed LINQ queries over raw SQL
47
- Use appropriate query operators (Where, OrderBy, GroupBy)
48
- Consider database functions for complex operations
49
- Implement specifications pattern for reusable queries
50
51
## Change Tracking & Saving
52
53
- Use appropriate change tracking strategies
54
- Batch your SaveChanges() calls
55
- Implement concurrency control for multi-user scenarios
56
- Consider using transactions for multiple operations
57
- Use appropriate DbContext lifetimes (scoped for web apps)
58
59
## Security
60
61
- Avoid SQL injection by using parameterized queries
62
- Implement appropriate data access permissions
63
- Be careful with raw SQL queries
64
- Consider data encryption for sensitive information
65
- Use migrations to manage database user permissions
66
67
## Testing
68
69
- Use in-memory database provider for unit tests
70
- Create separate testing contexts with SQLite for integration tests
71
- Mock DbContext and DbSet for pure unit tests
72
- Test migrations in isolated environments
73
- Consider snapshot testing for model changes
74
75
When reviewing my EF Core code, identify issues and suggest improvements that follow these best practices.
76

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…