Tutorials Entity Framework Core Tutorial

Data Annotations in EF Core

Data Annotations in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.

On this page

Entity Framework Core Tutorial · Lesson 13 of 100

Data Annotations in EF Core

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Foundations · ~6 min · Module 2: Code First Approach

What is this?

Data annotations are attributes like [Required], [MaxLength], and [Column] on entity properties. EF Core reads them when building the model for schema and optional validation.

Why should you care?

ShopNest product names must be required and capped at 200 characters — annotations express that at the property without Fluent API boilerplate.

See it live — copy this example

Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).

public class Product
{
    public int Id { get; set; }

    [Required, MaxLength(200)]
    public string Name { get; set; } = "";

    [Range(0.01, 9999999)]
    public decimal Price { get; set; }

    [MaxLength(50)]
    public string Sku { get; set; } = "";
}

What happened?

  • Required maps to NOT NULL.
  • MaxLength sets nvarchar length.
  • Range can participate in validation before SaveChanges if you enable it.

Practice next

  1. Annotate Product and Customer.Email with Required and MaxLength.
  2. Run dotnet ef migrations add AnnotateProducts and read Up() for column changes.
  3. Attempt save with empty Name and observe validation behavior.
  4. Add [Column(TypeName = "decimal(18,2)")] on Price and regenerate migration.
  5. Apply [EmailAddress] on Customer.Email for API-level validation synergy.

Remember

Annotations configure columns and simple validation. Good for straightforward rules on properties. Complex relationships still prefer Fluent API.

ShopNest catalog constraints

Product.Name annotations enforce NOT NULL and length before bad rows reach SQL Server.

Outcome: Migration creates nvarchar(200) matching Flipkart-style title limits.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
How do you configure mappings (table names, column names, keys) using Data Annotations vs Fluent API?
Short answer: 🧩 Data Annotations (in the entity class): [Table("Users")] public class User Example code { [Key] public int Id { get; set; } [Column("UserName")] public string Name { get; set; } } 🧠…
Junior PDF Detailed
What is Database First approach?
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to vers…
Junior PDF Detailed
What is Database First approach?
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy data…
Mid PDF Detailed
How do you reverse engineer a database in EF Core (DB First) into entity classes & DbContext?
Short answer: Use the CLI or Package Manager Console: dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -o Models OR Scaffold-DbContext "YourConnectionString"…
Mid PDF Detailed
How does Code First handle schema changes in the database?
Short answer: EF Core uses migrations to track and apply schema changes: You modify your C# models. Use Add-Migration to create a migration class. Use Update-Database to apply the changes to the DB. EF Core compares the…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details