Temporal table using EF in .Net 6
jayamoorthi parasuraman
FSD|Azure|Cosmos, postgresql, docker,|AKS|DevOps|Dot.Net Core/.Net5/6/7/8 ||xunit,nunit, integration, graphQL,gRpc | EFCore|API |WCF| Angular/React |Microservices,DDD/TDD| Dapper | Sonar Mob: +919715783720/+6580537622
What is use of temporal table?
Temporal feature introduced in EF core 6 on .net 6 supported.
?
Install Dotnet Cli in our local env the project directory path from command on your terminal
dotnet tool update --global dotnet-ef
?
?
Create a LoginUser model class
public class LoginUser : BaseEntity, ISoftDelete
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public bool IsSoftDeleted { get; set; }
}
?Create a InventoryDbContext class and inherit DbContext base class from EntityFramework library.
OnModelCreating() method we specify that?LoginUser?table should be created as temporal table
?
?Add migration using PMC
领英推荐
migration class file generated has successfully
?Now, go to db and refresh it, let see the update loginuser table
[PeriodEnd] and [PeriodStart] field were added in the main table (LoginUser) and history LoginUserHistory table has been created.
How do readind data from history table?
You can use various EF LINQ extension methods to query the historical data.
Add new method for Getting history table from IUserRepository interface.
?
IUserRepository.cs
? public interface IUserRepository: IRepository<LoginUser>
{
Task<List<LoginUser>?> GetTemporalAllUsersQueryAsync(Guid id);
}
TemporalAll() extension method , It querying from history table.
public class UserRepository : BaseRepository<LoginUser>, IUserRepository
{
private readonly InventoryDbContext _inventoryDbContext;
public UserRepository(InventoryDbContext context) : base(context)
{
_inventoryDbContext = context;
}
public async Task<List<LoginUser>?> GetTemporalAllUsersQueryAsync(Guid id)
{
List<LoginUser>? users = await _inventoryDbContext.LoginUser
.TemporalAll()
.Where(x => x.Id == id).ToListAsync();
return users;
}
}
.NET Core| C# | Microservices | SQL | ReactJS | CosmosDB | AZURE | CI/CD | Docker | Kubernetes Conduct : [email protected]
7 个月Nice article When the temporal table clear/delete and recreated?