Entity Framework Tutorial
Introduction to Entity Framework
Entity Framework (EF) is an open-source object-relational mapper (ORM) framework for ADO.NET. It allows developers to interact with a database using .NET objects, eliminating the need for most of the data-access code that developers usually need to write. EF supports LINQ queries, change tracking, and schema migrations.
Setting Up Entity Framework
Before you start using Entity Framework, you need to set it up in your project. This involves installing the necessary packages and configuring your database connection.
Install Entity Framework Core using NuGet Package Manager:
Install-Package Microsoft.EntityFrameworkCore
Alternatively, you can use the .NET CLI:
dotnet add package Microsoft.EntityFrameworkCore
Creating a Model
An EF model includes entity classes and a context class that represents a session with the database, allowing you to query and save data.
Here's an example of a simple model:
public class Product { public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } }
Setting Up the DbContext
The DbContext
class is the main class that coordinates Entity Framework functionality for a given data model. Create a class that inherits from DbContext
and configure it to use your model.
public class AppDbContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("your_connection_string_here"); } }
Creating and Applying Migrations
Migrations provide a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database.
Create an initial migration:
Add-Migration InitialCreate
Apply the migration to the database:
Update-Database
Querying Data
Once your model is set up and your database is created, you can start querying data using LINQ queries.
using (var context = new AppDbContext()) { var products = context.Products.ToList(); foreach (var product in products) { Console.WriteLine($"Product: {product.Name}, Price: {product.Price}"); } }
Inserting Data
You can add new records to the database by creating instances of your entity classes and adding them to the relevant DbSet
.
using (var context = new AppDbContext()) { var product = new Product { Name = "New Product", Price = 9.99m }; context.Products.Add(product); context.SaveChanges(); }
Updating Data
To update data, retrieve the record, modify the properties, and call SaveChanges
.
using (var context = new AppDbContext()) { var product = context.Products.First(); product.Price = 19.99m; context.SaveChanges(); }
Deleting Data
To delete a record, retrieve it and pass it to the Remove
method of the DbSet
.
using (var context = new AppDbContext()) { var product = context.Products.First(); context.Products.Remove(product); context.SaveChanges(); }
Conclusion
Entity Framework is a powerful tool for data access in .NET applications. By following this tutorial, you should now have a basic understanding of how to set up and use EF for common data operations such as querying, inserting, updating, and deleting data.