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! 📝
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. 💡
Before we dive into DbSet properties, let's create a new ASP.NET Core Web Application (Empty) project.
dotnet new webapi -n MyApp
cd MyAppNow, let's add Entity Framework Services and Microsoft.EntityFrameworkCore.SqlServer to our project.
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServerA 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.
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.
Entity classes are simple Plain Old CLR Objects (POCOs) that represent tables in the database. Let's create a Person class.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}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.
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);
}
}Finally, let's register our MyDbContext class in the Startup.cs file.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyAppDb")));
}Now that we've set up everything, let's create a simple API controller that can add, retrieve, update, and delete Person entities.
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)
}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! 🎉