Code First vs Database First in C#: Key Differences and When to Use
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#.
| Factor | Code First | Database First |
|---|---|---|
| Starting Point | Write C# classes first | Start with existing database schema |
| Database Creation | Database is generated from code | Database already exists |
| Schema Control | Full control via code | Control via database design |
| Workflow | Code changes update database | Database changes update code |
| Best For | New projects or domain-driven design | Existing databases or legacy systems |
| Tooling | Uses migrations to update DB | Uses 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.
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}"); } } } }
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.
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}"); } } } }
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.