Base entity
In every database table in our application, we have CreatedAt, UpdatedAt, CreatedBy and UpdatedBy columns.
Manually managing these fields can bring errors, it`s why it should be populated automatically.
When we add a new entity model, we just have to add BaseEntity base class inheritance.
public abstract class BaseEntity
{
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public int? CreatedBy { get; set; } = null;
public int? UpdatedBy { get; set; } = null;
}
Note: the class is identified as abstract, we can't instantiate directly a BaseEntity. It must be inherited from another class. CreatedBy and UpdatedBy are nullable, because sometimes we can add entities in database and a user is not necessarily logged.