
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Data Access in ASP.NET Core with Entity Framework
Entity Framework is an ORM (object-relational mapper) framework that makes it easy to create, retrieve, update, or delete data from relational databases. Using Entity Framework, you can work with C# objects that abstract the database related code, so you rarely have to deal with raw SQL.
The following figure illustrates where the Entity Framework fits into a layered architecture.
Entity Framework Core (EF Core) is the new version of Entity Framework 6. Similar to .NET Core, EF Core is a lightweight, open-source, and cross-platform version of Entity Framework. It is developed to be used with .NET Core applications.
To integrate EF Core into your ASP.NET Core applications with SQL Server, you need to use the NuGet packages for the database provider and the EF Core tools. For example, if we want to use SQL Server as the database, we will install the Microsoft.EntityFrameworkCore.SqlServer NuGet package. To install the EF Core tools, use the Microsoft.EntityFrameworkCore.Tools package.
An application using the Entity Framework works with the DbContext object, which is an essential part of the Entity Framework. It represents a session with the database, which is used to create, modify, and save the instances of the domain entities into the database.
The DbContext class represents the Unit of Work pattern, where a single object keeps track of the list of objects affected by changes and coordinates the persistence with the database. It performs the following responsibilities:
Manage the connection with the database
Set up the domain model and the relationships between them
Perform CRUD (Create, Read, Update, Delete) operations
Keep track of changes in a transaction
Cache the data to improve performance
The DbContext class for your application is created by deriving from the Microsoft.EntityFrameworkCore.DbContext class. The derived class specifies which entities are included in your data model. Here is an example that illustrates this.
using ContosoUniversity.Models; using Microsoft.EntityFrameworkCore; namespace ContosoUniversity.Data{ public class SchoolContext : DbContext{ public SchoolContext(DbContextOptions<SchoolContext> options) : base(options){ } public DbSet<Course> Courses { get; set; } public DbSet<Enrollment> Enrollments { get; set; } public DbSet<Student> Students { get; set; } } }
The DbContext object provides helper methods to query, modify, delete, and save the data to and from the database.
// Read data from the database private static void Main(string[] args){ var context = new SchoolContext(); var studentsWithSameName = context.Students .Where(s => s.FirstName == "David") .ToList(); } // Modify and save the data private static void Main(string[] args){ var context = new SchoolContext(); var david = context.Students .Where(s => s.FirstName == "David") .First(); david.Grade = "A"; context.SaveChanges(); } // Delete the data private static void Main(string[] args){ var context = new SchoolContext(); var david = context.Students .Where(s => s.FirstName == "David") .First(); context.Students.Remove(david); context.SaveChanges(); }