Advanced Topics

Intermediate

🧩 Advanced Topics

🔐 Data Annotations vs Fluent API

You can configure your model using attributes or the Fluent API.

🏷️ Data Annotations

[Required]
[StringLength(100)]
public string Name { get; set; }

🧬 Fluent API

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>()
        .Property(p => p.Name)
        .IsRequired()
        .HasMaxLength(100);
}

🔄 Tracking vs No-Tracking Queries

| Behavior       | Description                                          |
|----------------|------------------------------------------------------|
| Tracking       | Changes to objects are tracked for updates           |
| No-Tracking    | Objects are read-only and not tracked                |

Use AsNoTracking() for better performance in read-heavy scenarios.