Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2021 12:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Entity Framework Core - DbFunction
public class AuditLog
public Guid Id { get; set; } = Guid.NewGuid();
public DateTimeOffset Created { get; set; } = DateTimeOffset.UtcNow;
public string LogType { get; set; }
public string Data { get; set; }
public static class DatabaseFunctions
public static string JsonValue(string source, string path) => throw new NotSupportedException();
public static void Configure(ModelBuilder modelBuilder)
.HasTranslation(args => SqlFunctionExpression.Create("JSON_VALUE", args, typeof(string), null));
public class DataContext : DbContext
//pozostała zawartość
protected override void OnModelCreating(ModelBuilder modelBuilder)
FROM [AuditLogs]
WHERE JSON_VALUE([Data], N'$.ProductId') = '1612D8E7-8A18-41AD-8BFA-100E1E2D3854'
private static async Task Show()
await using var db = new DataContext();
var logs = await db.AuditLogs
.Where(l => DatabaseFunctions.JsonValue(l.Data, "$.ProductId") == _productId.ToString())
foreach (var auditLog in logs)
Console.WriteLine($"Log: {auditLog.LogType}, Date: {auditLog.LogType}, Data: {auditLog.Data}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment