Skip to main content

Code First Approach

C# Entity Framework's Code First approach is a fantastic technique that allows developers to build their databases based on their code. Instead of writing SQL code to create a database and its tables, you write C# classes. The Entity Framework then translates these classes into SQL and creates the database for you. This approach is especially useful for developers who prefer to work in code rather than dealing with the database directly.

What is Code First Approach?

Code First is one of three methods in Entity Framework that you can use to build your database. The other two are Database First and Model First. Code First is a popular choice because it enables you to work in a domain-oriented design pattern. This means you can focus on the business requirements and let the Entity Framework handle the database.

Setting Up a Code First Project

To get started with Code First, you'll first need to install the Entity Framework NuGet package. Open your Package Manager Console and run the following command:

Install-Package EntityFramework

After installation, you can start creating your database context and entity classes. The database context is a bridge between your entities (classes) and the database. It allows you to query and save data.

Here's a simple example of a database context:

public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}

In this example, BloggingContext is your database context, and Blog and Post are your entities. Each DbSet represents a table in your database.

Creating Entities

Entities in Code First are simply your domain classes. They don't need to inherit from any special base class or interface. Here's an example of what the Blog and Post classes might look like:

public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}

In these classes, each property represents a column in the table. The virtual keyword allows Entity Framework to use lazy loading, which means that related entities are only loaded when you access them.

Creating the Database

Once you have your context and entities set up, you can create your database. Simply create a new instance of your context and call a method on it. Here's an example:

using (var db = new BloggingContext())
{
db.Blogs.Add(new Blog { Name = "My Blog" });
db.SaveChanges();
}

In this code, a new Blog is added to the Blogs table and saved to the database. If the database doesn't exist, Entity Framework will create it for you.

Code First Migrations

When you make changes to your entities, you'll need to update your database to reflect these changes. Entity Framework provides a feature called Migrations to handle this. Migrations allow you to keep your database in sync with your code by preserving data.

To enable migrations, open your Package Manager Console and run the following command:

Enable-Migrations

When you make changes to your entities, you can add a new migration with the following command:

Add-Migration "DescriptiveName"

And to update your database with the migration, you run:

Update-Database

The Code First approach in Entity Framework is a powerful tool that allows you to build your database based on your code. It's a great option for developers who prefer to work in a domain-oriented design pattern and let the framework handle the database.