Entity Framework Configuration

This document covers the various configuration options available for Entity Framework (EF) in .NET, allowing you to tailor its behavior to your specific application needs.

Configuring Entity Framework

Entity Framework offers several ways to configure its behavior, including:

1. Using Configuration Files

The most common way to configure EF is through the <entityFramework> section in your application's configuration file (App.config for desktop applications, Web.config for web applications).

Connection Strings

Your database connection string must be defined in the configuration file. EF uses this to connect to your data source.

<configuration> <connectionStrings> <add name="MyDatabaseContext" connectionString="Server=.\SQLEXPRESS;Database=MyDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings> <entityFramework> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> </providers> </entityFramework> </configuration>

Provider Registration

The <entityFramework> section also registers the EF provider for your database. For SQL Server, this typically looks like:

<entityFramework> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> </providers> </entityFramework>

2. Fluent API Configuration

The Fluent API provides a powerful, code-based way to configure EF. You typically implement this by overriding the OnModelCreating method in your DbContext class.

Mapping Entity Properties

You can map properties to columns, specify data types, lengths, and more.

using System.Data.Entity; using System.ComponentModel.DataAnnotations.Schema; public class MyDbContext : DbContext { public DbSet<Product> Products { get; set; } public MyDbContext() : base("name=MyDatabaseContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Map Product entity to a specific table name modelBuilder.Entity<Product>() .ToTable("InventoryProducts"); // Configure properties modelBuilder.Entity<Product>() .Property(p => p.Name) .IsRequired() .HasMaxLength(100); modelBuilder.Entity<Product>() .Property(p => p.Price) .HasColumnType("decimal(18, 2)"); // Configure primary key modelBuilder.Entity<Product>() .HasKey(p => p.Id); // Define relationships (example: one-to-many) modelBuilder.Entity<Category>() .HasMany(c => c.Products) .WithRequired(p => p.Category) .HasForeignKey(p => p.CategoryId); base.OnModelCreating(modelBuilder); } } public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int CategoryId { get; set; } public Category Category { get; set; } } public class Category { public int CategoryId { get; set; } public string Name { get; set; } public ICollection<Product> Products { get; set; } }

Conventions

EF uses a set of default conventions. You can override these using the Fluent API or by creating custom conventions.

Tip: Overriding conventions in code (Fluent API) is generally preferred over modifying configuration files for maintainability and testability.

3. Entity Framework Core Configuration

For Entity Framework Core (EF Core), the configuration is handled exclusively through code. The DbContext.OnConfiguring method is used for basic options like specifying the database provider and connection string, while DbContext.OnModelCreating is used for model configuration.

EF Core OnConfiguring Example

using Microsoft.EntityFrameworkCore; public class MyCoreDbContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=MyCoreDatabase;Trusted_Connection=True;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { // EF Core model configuration using conventions or Fluent API modelBuilder.Entity<Product>().HasKey(p => p.Id); modelBuilder.Entity<Product>().Property(p => p.Name).IsRequired().HasMaxLength(100); } }

Common Configuration Scenarios

Understanding these configuration methods is crucial for effectively using Entity Framework to interact with your database.