Introduction to Entity Framework Core for CRUD Operations using ASP.NET Core Web API
Entity Framework Core (EF Core) is an open-source object-relational mapping framework for .NET applications. It is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. EF Core can be used to access databases using conventional SQL or with LINQ queries. EF Core enables developers to efficiently create data access applications with the latest technologies.
Getting Started with Entity Framework Core
To get started with EF Core in an ASP.NET Core Web API project, install the Microsoft.EntityFrameworkCore.SqlServer
NuGet package. This package contains the EF Core provider for Microsoft SQL Server. Once the package is installed, you can use EF Core to access a SQL Server database.
Creating the Database Context
The next step is to create a database context. The database context is used to interact with the database. It is a class that derives from Microsoft.EntityFrameworkCore.DbContext
and contains a set of DbSet properties that represent the entities in the database. For example, the following code creates a database context with a Users
entity.
public class MyDbContext : DbContext
{
public DbSet Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection string");
}
}
Migrations
Once the database context is created, you can use the EF Core migrations feature to automatically create the database and its tables. To do this, use the dotnet ef
CLI tool to create a migration. The migration will contain the code to create the database tables.
dotnet ef migrations add InitialMigration
Once the migration is created, you can use the dotnet ef
CLI tool to update the database with the new migration.
dotnet ef database update
This will create the database and its tables. The tables will be created based on the entities defined in the database context.
CRUD Operations
Once the database is created, you can use EF Core to perform CRUD (Create, Read, Update, and Delete) operations. To create a new entity, use the Add
method of the DbSet
property. For example, the following code creates a new User
entity.
using (var context = new MyDbContext())
{
var user = new User();
user.Name = "John Doe";
user.Email = "[email protected]";
context.Users.Add(user);
context.SaveChanges();
}
To read an entity, use the Find
method of the DbSet
property. For example, the following code reads a User
entity with the specified ID.
using (var context = new MyDbContext())
{
var user = context.Users.Find(1);
Console.WriteLine(user.Name);
}
To update an entity, use the Update
method of the DbSet
property. For example, the following code updates a User
entity.
using (var context = new MyDbContext())
{
var user = context.Users.Find(1);
user.Name = "Jane Doe";
user.Email = "[email protected]";
context.Users.Update(user);
context.SaveChanges();
}
To delete an entity, use the Remove
method of the DbSet
property. For example, the following code deletes a User
entity.
using (var context = new MyDbContext())
{
var user = context.Users.Find(1);
context.Users.Remove(user);
context.SaveChanges();
}
Tips
- Use the
dotnet ef
CLI tool to create and update the database with EF Core migrations. - Use the
Add
,Find
,Update
, andRemove
methods of theDbSet
property to perform CRUD operations. - Always call the
SaveChanges
method of the database context to persist changes to the database.
Conclusion
Using Entity Framework Core, you can quickly and easily create data access applications with the latest technologies. It is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. EF Core can be used to access databases using conventional SQL or with LINQ queries. With EF Core, you can use the dotnet ef
CLI tool to create migrations and then use the Add
, Find
, Update
, and Remove
methods of the DbSet
property to perform CRUD operations.