/EFCore.CustomDbContext

A simple library that customizes DbContext

Primary LanguageC#MIT LicenseMIT

EFCore.CustomDbContext

Nuget-Badges

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

Installation

Execute the following command in the terminal:

dotnet add package EFCore.CustomDbContext

Usage

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.