Automatic Timestamps
Entities can implement IHaveTimeStamp to have creation and update timestamps automatically set by the EntityManager:
public interface IHaveTimeStamp
{
DateTimeOffset? CreatedAtUtc { get; set; }
DateTimeOffset? UpdatedAtUtc { get; set; }
}
Example Entity
public class Article : IHaveOwner<string>, IHaveTimeStamp
{
public string Id { get; set; }
[DataOwner]
public string OwnerId { get; set; }
public DateTimeOffset? CreatedAtUtc { get; set; }
public DateTimeOffset? UpdatedAtUtc { get; set; }
public string Title { get; set; }
public string Content { get; set; }
string IHaveOwner<string>.Owner => OwnerId;
void IHaveOwner<string>.SetOwner(string owner) => OwnerId = owner;
}
How It Works
When using EntityManager<TEntity, TKey>:
- On Add:
CreatedAtUtcis automatically set toISystemTime.UtcNowwhen an entity implementingIHaveTimeStampis added - On Update:
UpdatedAtUtcis automatically set toISystemTime.UtcNowwhen the entity is updated
// Adding - CreatedAtUtc is set automatically
var article = await manager.AddAsync(new Article {
Title = "My Article",
Content = "Content here"
});
Console.WriteLine(article.CreatedAtUtc); // 2025-01-15T10:30:00Z
// Updating - UpdatedAtUtc is set automatically
article.Title = "Updated Title";
article = await manager.UpdateAsync(article);
Console.WriteLine(article.UpdatedAtUtc); // 2025-01-15T11:45:00Z
Timestamps are only set if an
ISystemTimeservice is registered in the DI container. This is typically done automatically in ASP.NET Core applications viaAddSystemTime().