Mid EF Core

How do you configure mappings (table names, column names, keys) using Data Annotations vs Fluent API?

🧩 Data Annotations (in the entity class):

[Table("Users")]

public class User

[Key]

public int Id { get; set; }

[Column("UserName")]

public string Name { get; set; }

🧠 Fluent API (in OnModelCreating):

modelBuilder.Entity<User>()

.ToTable("Users")

.HasKey(u => u.Id);

modelBuilder.Entity<User>()

.Property(u => u.Name)

.HasColumnName("UserName");

📌 Fluent API is more powerful and flexible, preferred for complex configurations.

More from Entity Framework Core Tutorial

All questions for this course