ASP.NET Tutorial: Understanding DbSet Properties 🎯

beginner
22 min

ASP.NET Tutorial: Understanding DbSet Properties 🎯

Welcome to our comprehensive guide on DbSet Properties in ASP.NET! In this lesson, we'll dive deep into the world of Entity Framework, a powerful Object-Relational Mapper (ORM) used in ASP.NET for handling database interactions. Let's get started! 📝

What are DbSet Properties?

DbSet properties are a fundamental part of Entity Framework, representing a set of entities of a specific type. They allow your application to interact with the database, fetch, add, update, and delete records, all in a simple and efficient manner. 💡

Setting up a Project

Before we dive into DbSet properties, let's create a new ASP.NET Core Web Application (Empty) project.

sh
dotnet new webapi -n MyApp cd MyApp

Adding Entity Framework

Now, let's add Entity Framework Services and Microsoft.EntityFrameworkCore.SqlServer to our project.

sh
dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Creating a DbContext Class

A DbContext is a class that manages the lifecycle of entities and change-tracking. Create a new class called MyDbContext that inherits from Microsoft.EntityFrameworkCore.DbContext.

csharp
using Microsoft.EntityFrameworkCore; using MyApp.Models; public class MyDbContext : DbContext { public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } public DbSet<Person> People { get; set; } }

In the above code, we've created a DbSet<Person> property called People. This property represents a set of Person entities, where Person is a class that we'll create next.

Creating Entity Classes

Entity classes are simple Plain Old CLR Objects (POCOs) that represent tables in the database. Let's create a Person class.

csharp
public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }

Configuring the DbContext

Now, let's configure our MyDbContext class to use SQL Server as our database provider. Create a new class called DbContextConfigurer and add the following code.

csharp
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using System.Collections.Generic; public class DbContextConfigurer : IDesignTimeDbContextFactory<MyDbContext> { public MyDbContext CreateDbContext(string[] args) { var builder = new DbContextOptionsBuilder<MyDbContext>(); builder.UseSqlServer( "Server=(localdb)\\mssqllocaldb;Database=MyAppDb;Trusted_Connection=True;"); return new MyDbContext(builder.Options); } }

Registering the DbContext

Finally, let's register our MyDbContext class in the Startup.cs file.

csharp
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyAppDb"))); }

Putting It All Together

Now that we've set up everything, let's create a simple API controller that can add, retrieve, update, and delete Person entities.

csharp
using Microsoft.AspNetCore.Mvc; using System.Linq; using MyApp.Models; using MyApp.Data; [Route("api/[controller]")] [ApiController] public class PeopleController : ControllerBase { private readonly MyDbContext _context; public PeopleController(MyDbContext context) { _context = context; } // ... (CRUD methods omitted for brevity) }

Quiz

Quick Quiz
Question 1 of 1

What does a DbSet property represent in Entity Framework?

That's it for this lesson! You've learned about DbSet properties in ASP.NET and have a working example to build upon. In the next lessons, we'll dive deeper into Entity Framework and explore more features. Happy coding! 🎉