Performing CRUD Operations with Entity Framework

Tutorial 5 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to provide a detailed guide on how to perform CRUD (Create, Read, Update, Delete) operations using Entity Framework, a widely used ORM (Object-Relational Mapping) framework for .NET.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the basics of Entity Framework
- Perform CRUD operations using Entity Framework
- Understand best practices in using Entity Framework

Prerequisites

  • Basic knowledge of C#
  • Basic understanding of SQL
  • Visual Studio installed on your machine

2. Step-by-Step Guide

Entity Framework (EF) is an open-source ORM framework for .NET applications supported by Microsoft. EF enables developers to work with data using objects of domain-specific classes without focusing on the underlying database tables and columns where this data is stored.

To perform CRUD operations using EF, we will use the DbContext class, which is the primary class that is responsible for interacting with the database.

Creating Data (C)

To create new records in the database using Entity Framework, you can create an instance of the entity and add it to the context. Then, call the SaveChanges() method to save the record to the database.

Reading Data (R)

To read data from the database, you can use LINQ queries. Entity Framework converts these queries into SQL and sends them to the database.

Updating Data (U)

For updating data, first, you need to retrieve the record, make changes to its properties, and then call the SaveChanges() method.

Deleting Data (D)

To delete a record, first, you have to retrieve it, then remove it from the context, and finally call SaveChanges().

3. Code Examples

Let's assume we have a Blog entity.

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }
}

Create

using (var context = new BloggingContext())
{
    var blog = new Blog { Name = "New Blog" };
    context.Blogs.Add(blog);
    context.SaveChanges();
}

Here, we create a new instance of Blog, add it to the Blogs collection in our context, and then call SaveChanges() to save this new blog to the database.

Read

using (var context = new BloggingContext())
{
    var blogs = context.Blogs.ToList();
}

We use LINQ to retrieve all blogs from the database.

Update

using (var context = new BloggingContext())
{
    var blog = context.Blogs.Find(1);
    blog.Name = "Updated Blog";
    context.SaveChanges();
}

After retrieving the blog we want to update, we change its Name property and then call SaveChanges().

Delete

using (var context = new BloggingContext())
{
    var blog = context.Blogs.Find(1);
    context.Blogs.Remove(blog);
    context.SaveChanges();
}

We retrieve the blog, remove it from the context, and then call SaveChanges() to delete it from the database.

4. Summary

In this tutorial, we learned about Entity Framework and how to perform CRUD operations using it. We saw how to create, read, update, and delete records from the database.

5. Practice Exercises

Exercise 1

Create a new Post entity and add it to the Posts collection in our BloggingContext.

Exercise 2

Retrieve all posts from the Posts collection in our BloggingContext.

Exercise 3

Update a post in the Posts collection in our BloggingContext.

Exercise 4

Delete a post from the Posts collection in our BloggingContext.