Entity Framework Mapping

.NET Data Access Concepts - Microsoft Learn

This document explores the fundamental concepts of mapping in Entity Framework (EF), focusing on how to translate between your .NET domain models and your relational database schema. Effective mapping is crucial for building robust and maintainable data access layers.

Understanding Mapping in Entity Framework

Entity Framework provides a powerful object-relational mapping (ORM) system. This means it allows you to work with your data as if it were objects in your .NET code, abstracting away much of the complexity of SQL and relational databases. The core of this abstraction lies in the mapping process.

What is Mapping?

Mapping defines the relationship between:

Types of Mapping Strategies

Entity Framework supports several approaches to defining these mappings:

1. Convention-Based Mapping (Code First)

In the Code First approach, you define your entity classes first. Entity Framework infers the database schema and mappings based on a set of conventions. This is often the quickest way to get started.

Key Conventions:

Example: A class named Product with a property ProductId will typically map to a table named Products with a primary key column named ProductId.

2. Data Annotations (Code First)

While conventions are useful, you often need more explicit control. Data Annotations allow you to decorate your entity classes with attributes to specify mapping details directly in your code.

Common Attributes:

C# Example with Data Annotations

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

    [Table("Customers")]
    public class Customer
    {
        [Key]
        [Column("CustomerID")]
        public int Id { get; set; }

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

        [ForeignKey("Country")]
        public int CountryId { get; set; }
        public virtual Country Country { get; set; } // Navigation property
    }
            

3. Fluent API (Code First / Model First)

For the most complex mapping scenarios or when you prefer to configure mapping outside of your entity classes, the Fluent API is the most powerful option. You configure mappings in a derived DbContext class using the OnModelCreating method.

C# Example with Fluent API

    using Microsoft.EntityFrameworkCore;

    public class AppDbContext : DbContext
    {
        public DbSet Customers { get; set; }
        public DbSet Countries { get; set; }

        public AppDbContext(DbContextOptions options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure Customer table
            modelBuilder.Entity()
                .ToTable("Customers")
                .HasKey(c => c.Id);

            modelBuilder.Entity()
                .Property(c => c.Id)
                .HasColumnName("CustomerID");

            modelBuilder.Entity()
                .Property(c => c.Name)
                .IsRequired()
                .HasMaxLength(100);

            // Configure relationship with Country
            modelBuilder.Entity()
                .HasOne(c => c.Country)
                .WithMany(co => co.Customers)
                .HasForeignKey(c => c.CountryId);
        }
    }
            

Database First vs. Code First

Entity Framework also supports a Database First approach, where you can generate entity classes and a DbContext from an existing database. The mapping is defined in an EDMX file or can be scaffolded into code.

Key Mapping Concepts

Best Practices

Mastering Entity Framework mapping is a cornerstone of efficient .NET data access development. By understanding these concepts, you can build applications that seamlessly interact with your databases.