EF Core Setup

Back

Loading concept...

🏗️ Entity Framework Core Setup

Your Database’s Best Friend


🎬 The Story: Building a Magic Library

Imagine you have a magic library 📚. This library can store anything—books, movies, games. But here’s the problem: the library speaks a weird language (SQL), and you only speak C#!

Entity Framework Core (EF Core) is your magical translator. It lets you talk to the library in C#, and it translates everything into the library’s language automatically!


🌟 What is EF Core?

EF Core is like having a robot assistant that:

  • 🗣️ Translates your C# code into database language
  • 📦 Fetches data and turns it into C# objects
  • ✏️ Saves your changes back to the database

Simple Example:

// Instead of writing scary SQL:
// SELECT * FROM Books WHERE Id = 1

// You write friendly C#:
var book = context.Books.Find(1);

That’s it! EF Core handles the translation. You focus on your app, not database syntax.


🎯 The Big Picture

graph TD A["Your C# Code"] --> B["DbContext"] B --> C["DbSet Collections"] C --> D["Entity Classes"] B --> E["Database Provider"] E --> F["#40;Database#41;"] D --> G["Configuration"] G --> H["Fluent API"] G --> I["Data Annotations"]

📦 DbContext: Your Database Remote Control

Think of DbContext as a TV remote control 📺. Just like a remote lets you control your TV (change channels, adjust volume), DbContext lets you control your database (add data, read data, save changes).

What Does DbContext Do?

Action Remote Control DbContext
Open Turn on TV Connect to database
Browse Change channels Query tables
Record Record a show Save new data
Save Confirm settings SaveChanges()

Creating Your Own Remote (DbContext)

public class LibraryContext : DbContext
{
    // This IS your remote control!

    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }

    // Connection settings go here
    protected override void OnConfiguring(
        DbContextOptionsBuilder options)
    {
        options.UseSqlServer(
            "Your-Connection-String");
    }
}

Key Parts:

  • 🎮 LibraryContext = Your custom remote
  • 📚 DbSet<Book> = The “Books channel”
  • ⚙️ OnConfiguring = Remote’s settings

📚 DbSet: Your Data Containers

If DbContext is the remote, then DbSet is like a playlist on your streaming service 🎵.

Each DbSet represents one table in your database:

public class LibraryContext : DbContext
{
    // Each DbSet = One table
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<Category> Categories { get; set; }
}

What Can You Do With DbSet?

// 🔍 Find all books
var allBooks = context.Books.ToList();

// ➕ Add a new book
context.Books.Add(new Book {
    Title = "C# Magic"
});

// 🗑️ Remove a book
context.Books.Remove(oldBook);

// 💾 Don't forget to save!
context.SaveChanges();

🔌 Database Provider Setup

A database provider is like a power adapter 🔌. Different countries have different plugs. Different databases have different providers!

Popular Providers

Database NuGet Package
SQL Server Microsoft.EntityFrameworkCore.SqlServer
SQLite Microsoft.EntityFrameworkCore.Sqlite
PostgreSQL Npgsql.EntityFrameworkCore.PostgreSQL
MySQL Pomelo.EntityFrameworkCore.MySql

Step 1: Install the Package

dotnet add package
    Microsoft.EntityFrameworkCore.SqlServer

Step 2: Configure the Connection

protected override void OnConfiguring(
    DbContextOptionsBuilder options)
{
    // SQL Server
    options.UseSqlServer(
        "Server=localhost;Database=MyLib;...");

    // Or SQLite (for simple apps)
    // options.UseSqlite(
    //     "Data Source=library.db");
}

Better Way: Use Dependency Injection

// In Program.cs or Startup.cs
builder.Services.AddDbContext<LibraryContext>(
    options => options.UseSqlServer(
        builder.Configuration
            .GetConnectionString("Default")
    )
);

🏠 Entity Classes: Your Data Blueprints

An entity is like a blueprint for a house 🏠. It describes what a “thing” looks like in your database.

Simple Entity Example

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public decimal Price { get; set; }
    public DateTime Published { get; set; }
}

This becomes a table:

Column Type
Id int (Primary Key)
Title nvarchar
Author nvarchar
Price decimal
Published datetime

Entity with Relationships

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }

    // Foreign Key
    public int AuthorId { get; set; }

    // Navigation Property
    public Author Author { get; set; }
}

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }

    // One author has many books
    public List<Book> Books { get; set; }
}

✨ Entity Configuration: Two Magical Ways

You can tell EF Core how to build your tables in two ways:

graph TD A["Entity Configuration"] --> B["Data Annotations"] A --> C["Fluent API"] B --> D["Use Attributes&lt;br&gt;[Required], [MaxLength]"] C --> E["Use Code in&lt;br&gt;OnModelCreating&#35;40;&#35;41;"]

🏷️ Data Annotations: Labels on Your Code

Data Annotations are like sticky notes 📝 you put directly on your entity classes.

Common Annotations

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

public class Book
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(200)]
    public string Title { get; set; }

    [Column("BookPrice")]
    public decimal Price { get; set; }

    [NotMapped]
    public string TempNote { get; set; }
}

Annotation Cheat Sheet

Annotation What It Does
[Key] This is the primary key
[Required] Cannot be null
[MaxLength(100)] Max 100 characters
[Column("Name")] Custom column name
[Table("MyBooks")] Custom table name
[NotMapped] Skip this property
[ForeignKey("AuthorId")] Link to another table

When to Use Data Annotations?

✅ Simple rules (required, max length) ✅ Quick and visible in the class ✅ Works for most common cases


🎨 Fluent API: The Artist’s Brush

Fluent API is like having a control panel 🎛️ where you configure everything in one place, with more power!

Where Does Fluent API Live?

public class LibraryContext : DbContext
{
    public DbSet<Book> Books { get; set; }

    protected override void OnModelCreating(
        ModelBuilder modelBuilder)
    {
        // All your configurations go here!
    }
}

Fluent API Examples

protected override void OnModelCreating(
    ModelBuilder modelBuilder)
{
    // Configure Book entity
    modelBuilder.Entity<Book>(entity =>
    {
        // Table name
        entity.ToTable("MyBooks");

        // Primary key
        entity.HasKey(b => b.Id);

        // Required + Max Length
        entity.Property(b => b.Title)
            .IsRequired()
            .HasMaxLength(200);

        // Column name
        entity.Property(b => b.Price)
            .HasColumnName("BookPrice");

        // Ignore property
        entity.Ignore(b => b.TempNote);
    });
}

Configuring Relationships

modelBuilder.Entity<Book>()
    .HasOne(b => b.Author)
    .WithMany(a => a.Books)
    .HasForeignKey(b => b.AuthorId)
    .OnDelete(DeleteBehavior.Cascade);

When to Use Fluent API?

✅ Complex configurations ✅ Keep entities clean (no attributes) ✅ Relationships and indexes ✅ Things annotations can’t do


⚔️ Data Annotations vs Fluent API

Feature Data Annotations Fluent API
Location On the class In OnModelCreating
Simplicity ⭐⭐⭐ ⭐⭐
Power ⭐⭐ ⭐⭐⭐
Relationships Limited Full control
Indexes Limited Full support

Pro Tip: You can use BOTH! Fluent API wins if there’s a conflict.


🚀 Complete Setup Example

1. Install Packages

dotnet add package
    Microsoft.EntityFrameworkCore.SqlServer
dotnet add package
    Microsoft.EntityFrameworkCore.Tools

2. Create Entity

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

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

    public decimal Price { get; set; }
}

3. Create DbContext

public class StoreContext : DbContext
{
    public StoreContext(
        DbContextOptions<StoreContext> options)
        : base(options) { }

    public DbSet<Product> Products { get; set; }

    protected override void OnModelCreating(
        ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .Property(p => p.Price)
            .HasPrecision(18, 2);
    }
}

4. Register in Program.cs

builder.Services.AddDbContext<StoreContext>(
    options => options.UseSqlServer(
        builder.Configuration
            .GetConnectionString("Default")
    )
);

5. Create Database

dotnet ef migrations add InitialCreate
dotnet ef database update

🎉 You Did It!

You now understand:

  • EF Core = Your C# to Database translator
  • DbContext = Your database remote control
  • DbSet = Your table containers
  • Providers = Your database adapters
  • Entities = Your data blueprints
  • Data Annotations = Quick labels on classes
  • Fluent API = Powerful configuration center

Next Step: Create your first entity, set up your DbContext, and watch EF Core work its magic! 🎩✨

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.