Module 5 - Chapter 2: Entity Framework Basics

Entity Framework (EF) is an open-source object-relational mapping (ORM) framework for ADO.NET, part of the .NET ecosystem. It serves as a bridge between the relational database world and the object-oriented world of .NET, allowing developers to work with data in the form of domain-specific objects and properties, without having to deal with the underlying database tables and columns directly. This chapter introduces the basics of Entity Framework, covering its two main approaches: Code-First and Database-First, and how to perform CRUD (Create, Read, Update, Delete) operations.

Mar 29, 2024

Entity Framework Basics

Entity Framework (EF) is an open-source object-relational mapping (ORM) framework for ADO.NET, part of the .NET ecosystem. It serves as a bridge between the relational database world and the object-oriented world of .NET, allowing developers to work with data in the form of domain-specific objects and properties, without having to deal with the underlying database tables and columns directly. This chapter introduces the basics of Entity Framework, covering its two main approaches: Code-First and Database-First, and how to perform CRUD (Create, Read, Update, Delete) operations.

Understanding Entity Framework

Entity Framework abstracts the database interaction layer, enabling developers to perform database operations without writing SQL queries directly. By using LINQ (Language Integrated Query), developers can query the database in a more intuitive way.

Code-First Approach

The Code-First approach allows developers to define their database schema directly in the code through POCO (Plain Old CLR Objects) classes. EF migrations can then be used to generate the database schema.

Defining Models

public class Blog { public int BlogId { get; set; } public string Name { get; set; } public 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 Blog Blog { get; set; } }

Configuring DbContext

DbContext is the primary class responsible for interacting with the database.
public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True"); } }

Using Migrations

Migrations track changes to the model and apply those changes to the database schema.
# Create a migration dotnet ef migrations add InitialCreate # Update the database dotnet ef database update

Database-First Approach

In the Database-First approach, the database schema is created directly in the database, and EF generates the models and DbContext based on the existing database schema.

Generating Models

EF provides tools to scaffold models from an existing database:
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

Performing CRUD Operations

With the context and models set up, you can perform CRUD operations:

Create

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

Read

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

Update

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

Delete

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

Summary

Entity Framework simplifies data manipulation in .NET applications by providing a high-level abstraction over the relational database system. Whether you prefer the Code-First approach to define your database schema in code or the Database-First approach to generate your code based on an existing database, EF streamlines the process of working with data. Understanding how to perform basic CRUD operations with EF is essential for developing data-driven applications in the .NET ecosystem.