.NET Web API & EF Core Sample

Overview

This sample demonstrates how to build a RESTful Web API using ASP.NET Core with Entity Framework Core for data access. It covers CRUD operations, async programming, and best practices for structuring a clean architecture.

Features

Getting Started

Install
Run
API Docs
git clone https://github.com/microsoft-samples/webapi-efcore-sample.git
cd webapi-efcore-sample
dotnet restore
dotnet build

Code Sample

using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .HasKey(p => p.Id);
    }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
}