Featured Article

AspNetCore Entity Framework Example Tutorial Build Web App

Kenneth Jul 13, 2026

ASP.NET Core, Microsoft's popular open-source web framework, presents a powerful platform for building modern, cloud-friendly, and efficient web applications. When combined with Entity Framework (EF), the go-to Object-Relational Mapping (ORM) tool for .NET, this duo provides a sleek and productive development experience. Let's explore a practical example that demonstrates how to set up an ASP.NET Core project with Entity Framework, connecting to a SQLite database, and creating a simple CRUD (Create, Read, Update, Delete) API.

the asp net core info sheet shows what it is like to work on an application
the asp net core info sheet shows what it is like to work on an application

Before we dive into the code, ensure you have the following installed on your system: .NET SDK, a code editor like Visual Studio or Visual Studio Code, and a database query tool such as DB Browser for SQLite for managing your SQLite database.

Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)

Setting Up the ASP.NET Core Project with Entity Framework

Let's start by creating an ASP.NET Core Web API project and adding the necessary packages for Entity Framework Core and the SQLite provider.

#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini
#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini

Create a new .NET Core Console Application project and install the required packages by running the following commands in your terminal:

dotnet new webapi -n AspNetCoreEFExample
cd AspNetCoreEFExample
dotnet add package Microsoft.EntityFrameworkCore.Sqlite

Creating the Model

What is the difference between ASP.NET and ASP.NET Core?
What is the difference between ASP.NET and ASP.NET Core?

Define a simple model, for example, a Blog class with properties like ID, Title, and Content. This model will correspond to a table in the SQLite database.

```csharp public class Blog { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } } ```

Creating the DbContext Class

Create a ApplicationDbContext class that derives from DbContext and configures the DbSet<Blog> property using the OnModelCreating method.

Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)

```csharp public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .HasKey(b => b.Id); base.OnModelCreating(modelBuilder); } } ```

Configuring the Database Connection

In the launchSettings.json file, add the connection string for the SQLite database in the application properties:

```json "ApplicationUrl": "https://localhost:5001;", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", " ConnectionStrings__DefaultConnection": "Data Source=blog.db" }, ```

Update the appsettings.json file to reflect the same connection string:

the architecture diagram for an application
the architecture diagram for an application

```json "ConnectionStrings": { "DefaultConnection": "Data Source=blog.db" }, ```

Registering the DbContext in Startup.cs

In the ConfigureServices method of the Startup class, add the following line to register the DbContext using the specified connection string:

Detailed ASP.NET MVC Pipeline
Detailed ASP.NET MVC Pipeline
Free Entity Framework Book
Free Entity Framework Book
GraphQL with Entity Framework Core || Basic Example || ASP.NET Core
GraphQL with Entity Framework Core || Basic Example || ASP.NET Core
ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload
ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core

```csharp services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); ```

Migrating the Database

Create a migration using the following command and then update the database:

```bash dotnet ef migrations add InitialCreate -c ApplicationDbContext dotnet ef database update -c ApplicationDbContext ```

Creating the CRUD API

Generate a Controller using the following command to create a basic CRUD API for the Blog model:

```bash dotnet ef controller api Blog -c ApplicationDbContext --api-controller -- vocalsize ```

Customize the controller actions to suit your needs, and now you have a functional CRUD API for managing blog posts using ASP.NET Core and Entity Framework.

The example above demonstrates the simplicity and productivity of using ASP.NET Core with Entity Framework. By following these steps, you can create efficient and maintainable data-driven web APIs. Happy coding! Don't forget to explore more advanced features and configurations that ASP.NET Core and Entity Framework offer to make the most out of these powerful tools.