Lesson 8 of 8
Mini Project: Blog API with EF Core
Build a complete Blog API: Posts, Comments, Tags with EF Core relationships (many-to-many tags, one-to-many comments), Fluent API configuration, migrations, LINQ queries with eager loading, cursor pagination, soft delete, and compiled queries for performance.
Quick Quiz - EF Core 8 & LINQ
Blog Domain Model & Queries
// ━━ Domain entities ━━
public class Post : BaseEntity
{
public string Title { get; set; } = "";
public string Slug { get; set; } = ""; // unique URL-friendly identifier
public string Content { get; set; } = "";
public string? Summary { get; set; }
public bool IsPublished { get; set; }
public DateTime? PublishedAt { get; set; }
public Guid AuthorId { get; set; }
public Author Author { get; set; } = null!;
public List<Comment> Comments { get; set; } = [];
public List<PostTag> PostTags { get; set; } = []; // many-to-many join table
public bool IsDeleted { get; set; }
public int ViewCount { get; set; }
}
public class Tag : BaseEntity
{
public string Name { get; set; } = "";
public string Slug { get; set; } = "";
public List<PostTag> PostTags { get; set; } = [];
}
public class PostTag // explicit join entity for many-to-many
{
public Guid PostId { get; set; }
public Post Post { get; set; } = null!;
public Guid TagId { get; set; }
public Tag Tag { get; set; } = null!;
}
// ━━ Production-quality queries ━━
public class PostQueryService(AppDbContext db)
{
// Compiled query for hot path (single post by slug)
private static readonly Func<AppDbContext, string, CancellationToken, Task<PostDto?>>
GetPostBySlug = EF.CompileAsyncQuery((AppDbContext ctx, string slug, CancellationToken _) =>
ctx.Set<Post>()
.Where(p => p.Slug == slug && p.IsPublished)
.Select(p => new PostDto(
p.Id, p.Title, p.Slug, p.Summary ?? "", p.Author.Name,
p.PublishedAt ?? DateTime.UtcNow,
p.PostTags.Select(pt => pt.Tag.Name).ToList()))
.FirstOrDefault());
public Task<PostDto?> GetBySlugAsync(string slug, CancellationToken ct) =>
GetPostBySlug(db, slug, ct);
// Paginated list - cursor on PublishedAt
public async Task<CursorPage<PostSummaryDto>> GetPublishedPostsAsync(
string? cursor, int limit, string? tag, CancellationToken ct)
{
DateTime? afterDate = cursor is null ? null
: new DateTime(long.Parse(
System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cursor))));
var query = db.Set<Post>()
.Where(p => p.IsPublished && !p.IsDeleted)
.Where(p => tag == null || p.PostTags.Any(pt => pt.Tag.Slug == tag))
.Where(p => afterDate == null || p.PublishedAt < afterDate)
.OrderByDescending(p => p.PublishedAt)
.Select(p => new PostSummaryDto(p.Id, p.Title, p.Slug, p.Summary ?? "",
p.Author.Name, p.PublishedAt!.Value,
p.PostTags.Select(pt => pt.Tag.Name).ToList(),
p.Comments.Count(c => !c.IsDeleted)));
var items = await query.Take(limit + 1).ToListAsync(ct);
var hasNext = items.Count > limit;
if (hasNext) items.RemoveAt(items.Count - 1);
var nextCursor = hasNext && items.Count > 0
? Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(
items.Last().PublishedAt.Ticks.ToString()))
: null;
return new CursorPage<PostSummaryDto>(items, nextCursor, hasNext, limit);
}
}
public class Author : BaseEntity { public string Name { get; set; } = ""; public string Email { get; set; } = ""; }
public class Comment : BaseEntity { public Guid PostId { get; set; } public string Content { get; set; } = ""; public bool IsDeleted { get; set; } }
record PostDto(Guid Id, string Title, string Slug, string Summary, string AuthorName, DateTime PublishedAt, List<string> Tags);
record PostSummaryDto(Guid Id, string Title, string Slug, string Summary, string AuthorName, DateTime PublishedAt, List<string> Tags, int CommentCount);Tip
Tip
Practice Mini Project Blog API with EF Core in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
.NET provides a unified platform for building various application types
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Blog API with EF Core from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Mini Project Blog API with EF Core is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.
Finished this lesson?