Using Entity Framework Core for Data Access
Introduction
Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. In this tutorial, we will explore how to use EF Core for data access in a .NET application, from setup to querying and saving data.
Setting Up EF Core
To get started, we need to set up EF Core in our .NET project.
Installing NuGet Packages
// Open your .NET project in Visual Studio
// Install the following NuGet packages:
// - Microsoft.EntityFrameworkCore
// - Microsoft.EntityFrameworkCore.SqlServer (or another database provider)
// - Microsoft.EntityFrameworkCore.Tools
// Use the NuGet Package Manager Console or the command line:
// Install-Package Microsoft.EntityFrameworkCore
// Install-Package Microsoft.EntityFrameworkCore.SqlServer
// Install-Package Microsoft.EntityFrameworkCore.Tools
Creating the Data Model
Define your data model by creating entity classes that represent the tables in your database.
Example Entity Class
using System.ComponentModel.DataAnnotations;
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public decimal Price { get; set; }
}
Creating the DbContext
Create a DbContext class that represents a session with the database and provides an API for performing CRUD operations.
Example DbContext Class
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionStringHere");
}
}
Migrations
Use migrations to create and update the database schema based on your data model.
Adding and Applying Migrations
// Open the NuGet Package Manager Console
// Add an initial migration
Add-Migration InitialCreate
// Apply the migration to the database
Update-Database
Querying Data
Use LINQ queries to retrieve data from the database.
Example Query
using System;
using System.Linq;
class Program
{
static void Main()
{
using (var context = new ApplicationDbContext())
{
var products = context.Products.ToList();
foreach (var product in products)
{
Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
}
}
}
}
Inserting Data
To insert data into the database, add new entities to the DbSet and call SaveChanges.
Example Insert Operation
using System;
class Program
{
static void Main()
{
using (var context = new ApplicationDbContext())
{
var newProduct = new Product
{
Name = "New Product",
Price = 9.99m
};
context.Products.Add(newProduct);
context.SaveChanges();
}
}
}
Updating Data
To update data, retrieve the entity, modify its properties, and call SaveChanges.
Example Update Operation
using System;
using System.Linq;
class Program
{
static void Main()
{
using (var context = new ApplicationDbContext())
{
var product = context.Products.First();
product.Price = 19.99m;
context.SaveChanges();
}
}
}
Deleting Data
To delete data, remove the entity from the DbSet and call SaveChanges.
Example Delete Operation
using System;
using System.Linq;
class Program
{
static void Main()
{
using (var context = new ApplicationDbContext())
{
var product = context.Products.First();
context.Products.Remove(product);
context.SaveChanges();
}
}
}
Conclusion
In this tutorial, you learned how to use Entity Framework Core for data access in a .NET application. We covered setting up EF Core, creating the data model and DbContext, performing migrations, and basic CRUD operations.