Introduction
Entity Framework (EF) Core is a lightweight, extensible, open-source, and cross-platform version of the popular Entity Framework data access technology. It serves as an Object-Relational Mapper (ORM) that enables .NET developers to work with a database using .NET objects, eliminating the need for most of the data-access code developers usually need to write.
In this article, we will walk through the process of setting up EF Core in an ASP.NET Core web application and performing basic CRUD operations.
Why Entity Framework Core?
- Productivity: Auto-generates SQL scripts and manages migrations.
- Maintainability: Makes code cleaner and more maintainable.
- Testability: Supports dependency injection and in-memory databases.
- Cross-platform: Works across Windows, macOS, and Linux.
Prerequisites
- Visual Studio 2022 or later (or VS Code with .NET SDK).
- .NET 6 or .NET 8 SDK installed.
- Basic knowledge of C# and ASP.NET Core MVC.
Step-by-Step Implementation
Step 1. Create an ASP.NET Core MVC Project
Create a new project in Visual Studio.
File > New > Project > ASP.NET Core Web App (Model-View-Controller)
Ensure .NET 6.0 or .NET 8.0 is selected.
Step 2. Install Entity Framework Core Packages
Using NuGet Package Manager Console.
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Step 3: Define the Data Model
Create a model class under Models folder.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
public int Age { get; set; }
}
Step 4: Configure DbContext
Create a class AppDbContext.cs:
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}
Register this context in Program.cs
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
)
);
Add the connection string in appsettings.json.
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=StudentDB;Trusted_Connection=True;"
}
}
Step 5: Add a Controller and Views
Use the scaffold tool to generate a controller and views.
Add > Controller > MVC Controller with views, using Entity Framework
Choose the Student model and AppDbContext.
Step 6: Run Migrations
Add-Migration InitialCreate
Update-Database
This creates the database and tables.
Best Practices
- Always validate input models.
- Use async methods for better performance.
- Use Repository Pattern for complex systems.
Challenges Faced
- Initial migration issues if connection strings are misconfigured.
- Model-property mismatches can throw runtime errors.
- Limited debugging options compared to raw SQL.
Future Scope
- Integrate EF Core with Azure SQL Database.
- Use EF Core with APIs and microservices.
- Explore advanced features like Query Tags, Global Filters, and Stored Procedures.
Author Information
- Name: Mohammed Altaf
- Expertise: C#, ASP.NET Core, ML, Data Engineering