Skip to content

Entity Framework Core Coding Conventions for CowPress and ToDoMoo

In developing CowPress and ToDoMoo, CacheCows adheres to specific coding conventions for Entity Framework Core (EF Core). These conventions help in maintaining a consistent, efficient, and scalable data access layer in our applications. This article outlines the key EF Core coding conventions used in our development process.

General EF Core Conventions

  • DbContext Usage: Use DbContext in a using statement to ensure proper disposal of resources.
  • Repository Pattern: Implement the repository pattern to abstract the data layer and decouple it from the business logic.
  • UnitOfWork Pattern: Use the UnitOfWork pattern for managing data operations and ensuring data integrity.

Model Configuration

  • Fluent API over Data Annotations: Prefer using Fluent API for model configuration for more flexibility and control.
  • Naming Conventions: Table names in plural form, column names in singular form. Stick to the database naming conventions if integrating with an existing database.

Migrations

  • Incremental Migrations: Regularly add incremental migrations instead of a big bang approach.
  • Migration Naming: Name migrations descriptively to reflect the changes made.
  • Version Control: Include migration files in version control to keep track of database schema changes.

Querying

  • LINQ Queries: Use LINQ to write queries, ensuring readability and maintainability.
  • Avoid N+1 Queries: Be aware of N+1 problems and optimize queries to reduce round trips to the database.
  • Eager Loading: Use eager loading (Include method) judiciously to avoid unnecessary data loading.

Performance

  • Batch Operations: Use batch operations for large data manipulation to improve performance.
  • AsNoTracking(): When read-only data is sufficient, use AsNoTracking() for performance optimization.

Error Handling

  • Handle EF Core Exceptions: Properly handle EF Core exceptions, providing meaningful error messages.
  • Logging: Log critical information during database operations for debugging purposes.

Testing

  • Unit Testing: Write unit tests for critical data operations.
  • Mocking DbContext: Use mocking frameworks to test DbContext and DbSet.

By following these EF Core coding conventions, we aim to maintain a robust and efficient data access strategy in our development process for CowPress and ToDoMoo.


Last update : November 17, 2023
Created : November 17, 2023