This library sets automatically the CreatedAt and UpdatedAt properties.
Credits for this page: https://www.entityframeworktutorial.net/faq/set-created-and-modified-date-in-efcore.aspx
Execute the following command in the terminal:
dotnet add package EFCore.CustomDbContext
Inherits from CustomBaseEntity
class:
public class Entity : CustomBaseEntity
{
public int Id { get; set; }
}
And then inherit from CustomDbContext
:
public class AppDbContext : CustomDbContext { }
By default, the library sets the current date with DateTime.Now
, this behavior can be changed:
public class AppDbContext : CustomDbContext
{
public override DateTime CreatedAt => DateTime.UtcNow;
public override DateTime UpdatedAt => DateTime.UtcNow;
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
And ready, use the SaveChangesAsync
method freely.