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.

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.

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.

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

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.

```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:

```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:









```csharp
services.AddDbContextMigrating 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.