DKNet

Read keys and tracked property values

Part of DKNet.EfCore.Extensions. Inspect model keys and original/current entry values without repeating metadata lookups.

Use the console-project setup in the Quick Start, then replace Program.cs with this example.

using Microsoft.EntityFrameworkCore;
using DKNet.EfCore.Extensions.Extensions;

using var connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source=:memory:");
connection.Open();
var o = new DbContextOptionsBuilder<DemoDb>();
o.UseSqlite(connection);
await using var db = new DemoDb(o.Options);
var item = new Item { Id = 7, Name = "Coffee" };
db.Attach(item);
var entry = db.Entry(item);
Console.WriteLine(db.GetPrimaryKeyProperties<Item>().Single()); // Id
Console.WriteLine(entry.GetEntityKeyValues()[nameof(Item.Id)]); // 7
Console.WriteLine(entry.GetOriginalValue(nameof(Item.Name))); // Coffee
Console.WriteLine(entry.IsNewEntity()); // False

public sealed class DemoDb(DbContextOptions<DemoDb> options) : DbContext(options)
{
    public DbSet<Item> Items => Set<Item>();
}
public sealed class Item
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
}

Key helpers resolve model primary-key metadata and read the entry original or current value for each property.

GetPrimaryKeyValues(object) and GetEntityKeyValues() return name/value dictionaries with ordinal case-insensitive keys. GetOriginalKeyValues and GetCurrentKeyValues return value sequences in metadata key order.

⚠️ Gotchas & limits