Entity Framework Core Mastery

Entity Conventions & Data Annotations

1 Views Updated 5/4/2026

Entity Conventions & Data Annotations

When using the Code-First approach, EF Core uses Conventions (smart defaults) to guess how your C# classes should map to SQL Tables. When those guesses are wrong, you can override them using Data Annotations—attributes placed directly on your C# properties.

1. Understanding Default Conventions

If you don't strictly define your database rules, EF Core assumes the following:

  • If a class is named User, it generates a table named Users (Pluralized).
  • If a property is named Id or UserId, it designates it as the Primary Key and configures it to Auto-Increment.
  • If a property is a string, it maps it to NVARCHAR(MAX) in SQL Server.
  • If a property is not nullable (e.g., int vs int?), it makes the SQL Column NOT NULL.

2. Applying Data Annotations

Relying purely on conventions often leads to bloated databases (like defining every string field as MAX length). We use attributes from System.ComponentModel.DataAnnotations to assert strict control.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

// Overrides the table name convention
[Table("tbl_CompanyEmployees")]
public class Employee
{
    // Explicitly declare the Primary Key
    [Key]
    public string EmployeeId { get; set; }

    // Overrides NVARCHAR(MAX) to NVARCHAR(50)
    // Marks the column as NOT NULL in the database
    [Required]
    [MaxLength(50)]
    public string FirstName { get; set; }

    // Maps the C# property "Cost" to a SQL column named "Calculated_Expense"
    // Forces the SQL precision to be 18 digits with 2 decimal places
    [Column("Calculated_Expense", TypeName = "decimal(18,2)")]
    public decimal Cost { get; set; }

    // This property exists in C# RAM, but will NEVER be created as a SQL Column!
    [NotMapped]
    public string FullName => $"{FirstName} {LastName}";
}

3. Database Validation vs UI Validation

Data Annotations are powerful because they pull double-duty. The [Required] and [MaxLength(50)] attributes don't just configure the SQL Database column during migrations; they also automatically trigger ASP.NET Core MVC/Web API validation responses! If a user submits a 51-character string, the API will reject it with a 400 Bad Request before EF Core ever attempts to save it.

4. Interview Mastery

Q: "Why do Senior Architects generally discourage using Data Annotations to configure EF Core, preferring the Fluent API instead?"

Architect Answer: "The core philosophy of Domain-Driven Design (DDD) dictates that your Core Domain Entities (the C# classes) should be completely pure and unaware of the infrastructure layer. By plastering `[Table('tbl_Users')]` and `[Column(TypeName)]` all over our C# objects, we are tightly coupling our Domain to arbitrary SQL Database concerns. Furthermore, Data Annotations are functionally limited. They cannot configure complex schemas like shadow properties, composite primary keys, or sophisticated index filtering. The Fluent API (`OnModelCreating`) removes all database mapping logic from the C# classes and stores it in centralized configuration files, keeping our entities clean and unlocking 100% of EF Core's feature set."

Entity Framework Core Mastery
1. Foundations & Architecture
Introduction to Object Relational Mapping (ORM) Entity Framework Core Architecture & Providers Setup and DbContext Integration Code-First vs Database-First Approaches Reverse Engineering Existing Databases (Scaffolding)
2. Code-First Modeling
Entity Conventions & Data Annotations The Fluent API Deep Dive (OnModelCreating) Primary Keys, Composite Keys, & Guids Required Properties & Database Defaults Value Conversions (Enums & Strongly Typed IDs)
3. Relational Architecture
One-to-Many Relationships & Foreign Keys One-to-One Relationships (Dependent Entities) Many-to-Many Relationships & Navigation Properties Owned Entity Types (Value Objects) Table-per-Hierarchy (TPH) Inheritance
4. Data Querying & LINQ
Basic LINQ Queries & IQueryable Execution Tracking vs No-Tracking Queries (Performance) Eager Loading vs Explicit Loading (Include) Lazy Loading Pitfalls & Proxies Client vs Server Evaluation Parsing
5. Manipulating Data (CUD)
Adding, Updating, and Removing Entities The ChangeTracker and Entity States Disconnected Entities in Web APIs Batch Updates and Deletes (.NET 7+)
6. Advanced Performance & Scale
Concurrency Tokens and Optimistic Locking Raw SQL Queries and Views (FromSqlRaw) Compiled Queries for High Throughput Interceptors (Logging & Auditing Data Changes) DbContext Pooling Mechanisms Managing Complex EF Core Migrations (CI/CD)