0
0
CsharpComparisonBeginner · 4 min read

Code First vs Database First in C#: Key Differences and When to Use

In C#, Code First means you write your classes first and the database is created from them, while Database First means you start with an existing database and generate classes from it. Both are ways to work with databases using Entity Framework but differ in workflow and control.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of Code First and Database First approaches in C#.

FactorCode FirstDatabase First
Starting PointWrite C# classes firstStart with existing database schema
Database CreationDatabase is generated from codeDatabase already exists
Schema ControlFull control via codeControl via database design
WorkflowCode changes update databaseDatabase changes update code
Best ForNew projects or domain-driven designExisting databases or legacy systems
ToolingUses migrations to update DBUses EDMX or scaffolding tools
⚖️

Key Differences

Code First lets you define your data model using C# classes and then creates the database schema based on those classes. This approach gives you full control over your code and lets you evolve the database through migrations as your classes change.

In contrast, Database First starts with an existing database. You generate C# classes that match the database tables and relationships. This is useful when working with legacy databases or when the database design is managed separately.

Code First is more flexible for new projects and supports a code-centric workflow, while Database First is better when the database is the source of truth and you want to keep your code in sync with it.

💻

Code First Example

This example shows how to define a simple model and context in Code First. The database will be created from this code.

csharp
using System;
using System.Data.Entity;

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ShopContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

class Program
{
    static void Main()
    {
        using (var db = new ShopContext())
        {
            db.Products.Add(new Product { Name = "Apple", Price = 0.5m });
            db.SaveChanges();

            foreach (var product in db.Products)
            {
                Console.WriteLine($"{product.Name}: ${product.Price}");
            }
        }
    }
}
Output
Apple: $0.5
↔️

Database First Equivalent

This example shows how you would use Database First by generating classes from an existing database. The code below assumes the classes were scaffolded and you use the context to read data.

csharp
using System;
using System.Data.Entity;

// Assume this class was generated from the existing database
public partial class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public partial class ShopContext : DbContext
{
    public virtual DbSet<Product> Products { get; set; }
}

class Program
{
    static void Main()
    {
        using (var db = new ShopContext())
        {
            foreach (var product in db.Products)
            {
                Console.WriteLine($"{product.Name}: ${product.Price}");
            }
        }
    }
}
Output
Apple: $0.5
🎯

When to Use Which

Choose Code First when you are starting a new project and want full control over your data model in code. It is great for agile development where the database schema evolves with your code.

Choose Database First when you have an existing database or when database design is managed by a separate team. It helps keep your code synchronized with the database schema without manually writing classes.

Key Takeaways

Code First starts with C# classes and creates the database from them.
Database First starts with an existing database and generates C# classes.
Use Code First for new projects and flexible schema evolution.
Use Database First for legacy databases or when DB design is separate.
Both approaches use Entity Framework but differ in workflow and control.