From c50ce81e532f84b8e72d805c94beea03c2257be8 Mon Sep 17 00:00:00 2001 From: eitam Date: Mon, 23 Mar 2026 10:10:12 -0700 Subject: [PATCH] feat: add C# EF Core example --- .github/workflows/valk-guard.yml | 1 + README.md | 2 ++ csharp/efcore/UserRepository.cs | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 csharp/efcore/UserRepository.cs diff --git a/.github/workflows/valk-guard.yml b/.github/workflows/valk-guard.yml index 50f74b6..88d0b48 100644 --- a/.github/workflows/valk-guard.yml +++ b/.github/workflows/valk-guard.yml @@ -36,6 +36,7 @@ jobs: **/*.sql **/*.go **/*.py + **/*.cs - uses: reviewdog/action-setup@v1 if: steps.changed.outputs.any_changed == 'true' diff --git a/README.md b/README.md index 76ae6fb..b7f333f 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ Expected result on `main`: ```text valk-guard-example/ +├── csharp/ +│ └── efcore/ ├── go/ │ ├── std/ │ └── goqu/ diff --git a/csharp/efcore/UserRepository.cs b/csharp/efcore/UserRepository.cs new file mode 100644 index 0000000..fd2e98f --- /dev/null +++ b/csharp/efcore/UserRepository.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace ValkGuardExample.Data; + +/// +/// Example repository demonstrating clean EF Core raw SQL usage. +/// All queries here should pass valk-guard with 0 findings. +/// +public class UserRepository +{ + private readonly AppDbContext _db; + + public UserRepository(AppDbContext db) + { + _db = db; + } + + /// Safe: bounded SELECT with WHERE and LIMIT. + public void GetActiveUserCount() + { + _db.Database.ExecuteSqlRaw( + "SELECT count(*) FROM users WHERE active = true"); + } + + /// Safe: DELETE with WHERE clause. + public async Task CleanupExpiredSessions() + { + await _db.Database.ExecuteSqlRawAsync( + @"DELETE FROM orders + WHERE status = 'expired' + AND created_at < NOW() - INTERVAL '90 days'"); + } + + /// Safe: parameterized UPDATE with WHERE. + public void DeactivateUser(int userId) + { + _db.Database.ExecuteSqlRaw( + "UPDATE users SET active = false WHERE id = {0}", userId); + } + + /// Safe: interpolated DELETE with WHERE. + public async Task RemoveUser(int userId) + { + await _db.Database.ExecuteSqlInterpolatedAsync( + $"DELETE FROM users WHERE id = {userId}"); + } +}