Idam.Libs.EF 7.0.0

Suggested Alternatives

Idam.EFTimestamps

Additional Details

This package was moved to Idam.EFTimestamps.

dotnet add package Idam.Libs.EF --version 7.0.0                
NuGet\Install-Package Idam.Libs.EF -Version 7.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Idam.Libs.EF" Version="7.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Idam.Libs.EF --version 7.0.0                
#r "nuget: Idam.Libs.EF, 7.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Idam.Libs.EF as a Cake Addin
#addin nuget:?package=Idam.Libs.EF&version=7.0.0

// Install Idam.Libs.EF as a Cake Tool
#tool nuget:?package=Idam.Libs.EF&version=7.0.0                

Idam.Libs.EF

NuGet .NET

Idam.Libs.EF is .Net Core (C#) for Entity Framework (EF) Utils.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Features

  • Soft delete (DeletedAt).
  • Timestamps (CreatedAt, UpdatedAt).
  • Custom Timestamps fields.

Both features support DateTime, UTC DateTime, and Unix Time Milliseconds format.

Example of Unix Time Milliseconds: currentmillis.com

Get started

run this command to install

Install-Package Idam.Libs.EF

or

dotnet tool install Idam.Libs.EF

Usage

Using Timestamps

  1. Add AddTimestamps() in your context.

    using Idam.Libs.EF.Extensions;
    
    public class MyDbContext : DbContext
    {
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChanges(acceptAllChangesOnSuccess);
        }
    
        public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }
    }
    
  2. Add an attribute (TimeStamps or TimeStampsUtc or TimeStampsUnix) to your entity. You can also implement an Interface (ITimeStamps or ITimeStampsUnix) according attribute you use.

    using Idam.Libs.EF.Attributes;
    using Idam.Libs.EF.Interfaces;
    
    /// BaseEntity
    public class BaseEntity
    {
        public int Id { get; set; }
        public string Name { get; set; } = default!;
        public string? Description { get; set; }
    }
    
    /// Using DateTime Format
    [TimeStamps]
    public class Doo : BaseEntity, ITimeStamps
    {
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
    
    /// Using UTC DateTime Format
    [TimeStampsUtc]
    public class UtcDoo : BaseEntity, ITimeStamps
    {
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
    
    /// Using Unix Format
    [TimeStampsUnix]
    public class Foo : ITimeStampsUnix
    {
        public long CreatedAt { get; set; }
        public long UpdatedAt { get; set; }
    }
    

Using SoftDelete

  1. Add AddTimestamps() and AddSoftDeleteFilter() in your context.

    using Idam.Libs.EF.Extensions;
    
    public class MyDbContext : DbContext
    {
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChanges(acceptAllChangesOnSuccess);
        }
    
        public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
        {
            ChangeTracker.AddTimestamps();
    
            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.AddSoftDeleteFilter();
    
            base.OnModelCreating(modelBuilder);
        }
    }
    
  2. Add an attribute (TimeStamps or TimeStampsUtc or TimeStampsUnix) to your entity. You can also implement an Interface (ISoftDelete or ISoftDeleteUnix) according attribute you use.

    using Idam.Libs.EF.Attributes;
    using Idam.Libs.EF.Interfaces;
    
    /// Using DateTime Format
    [TimeStamps]
    public class Doo : BaseEntity, ISoftDelete
    {
        public DateTime? DeletedAt { get; set; }
    }
    
    /// Using UTC DateTime Format
    [TimeStampsUtc]
    public class UtcDoo : BaseEntity, ISoftDelete
    {
        public DateTime? DeletedAt { get; set; }
    }
    
    /// Using Unix Format
    [TimeStampsUnix]
    public class Foo : BaseEntity, ISoftDeleteUnix
    {
        public long? DeletedAt { get; set; }
    }
    
Restore

The SoftDelete has a Restore() function, so you can restore the deleted data.

using Idam.Libs.EF.Extensions;

/// Your context
public class MyDbContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
}

/// Foo Controller
public class FooController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> RestoreAsync(Foo foo)
    {
        Foo restoredFoo = _context.Foos.Restore(foo);
        await context.SaveChangesAsync();
        
        return Ok(restoredFoo);
    }
}
ForceRemove

The SoftDelete has a ForceRemove() function, so you can permanently remove the data.

/// Foo Controller
public class FooController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> ForceRemoveAsync(Foo foo)
    {
        _context.Foos.ForceRemove(foo);
        await context.SaveChangesAsync();
        
        return Ok(restoredFoo);
    }
}
Trashed

The SoftDelete has a Trashed() function to check if current data is deleted.

/// Foo Controller
public class FooController
{
    public IActionResult IsDeletedFoo(Foo foo)
    {
        bool isDeleted = foo.Trashed();
        
        return Ok(isDeleted);
    }
}

The Trashed() function only shows when your entity implements an interface ISoftDelete or ISoftDeleteUnix.

Ignore global softdelete filter

By default the deleted data filtered from the query, if you want to get the deleted data you can ignore the global softdelete filter by using IgnoreQueryFilters().

/// Foo Controller
public class FooController
{
    readonly MyDbContext _context;

    public async Task<IActionResult> GetAllDeletedAsync()
    {
        var deletedFoos = await _context.Foos
            .IgnoreQueryFilters()
            .Where(x => x.DeletedAt != null)
            .ToListAsync();

        return Ok(deletedFoos);
    }
}

Using Custom TimeStamps fields

By default, the TimeStamps attribute uses CreatedAt, UpdatedAt, and DeletedAt as field names. It's possible to customize the TimeStamps fields.

  1. Create your own TimeStamps attribute.

    public class MyTimeStampsAttribute : TimeStampsAttribute
    {
        public override TimeStampsType TimeStampsType { get; set; } = TimeStampsType.UtcDateTime;
        public override string? CreatedAtField { get; set; } = "AddedAt";
        public override string? UpdatedAtField { get; set; } = "EditedAt";
        public override string? DeletedAtField { get; set; } = "RemovedAt";
    }
    
  2. Add the new attribute to your entity.

    [MyTimeStamps]
    public class Doo : BaseEntity
    {
        public DateTime AddedAt { get; set; }
        public DateTime EditedAt { get; set; }
        public DateTime? RemovedAt { get; set; }
    }
    

    Tips: Create your interface according to your own TimeStamps attribute.

Using just few TimeStamps fields

You can use just a few TimeStamps fields by filling in null or string empty to fields you don't use.

  1. Create your own TimeStamps attribute.

    public class MyTimeStampsAttribute : TimeStampsAttribute
    {
        public override TimeStampsType TimeStampsType { get; set; } = TimeStampsType.UtcDateTime;
        public override string? CreatedAtField { get; set; } = "";
        public override string? UpdatedAtField { get; set; } = "EditedAt";
        public override string? DeletedAtField { get; set; } = "RemovedAt";
    }
    
  2. Add the new attribute to your entity.

    [MyTimeStamps]
    public class Doo : BaseEntity
    {
        public DateTime EditedAt { get; set; }
        public DateTime? RemovedAt { get; set; }
    }
    

Using IGuidEntity

An Interface to implement Id as Guid instead of int.

using Idam.Libs.EF.Interfaces;

public class Foo : IGuidEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; } = default!;
    public string? Description { get; set; }
}

Migrating

Migrating from 2.1.0

  1. Add [TimeStampsUtc] or [TimeStampsUnix] attribute to your entities according to your TimeStamps data type before.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

- Use attribute.
- Support custom timestamps fields.
- Support DateTime, UTC DateTime, and Unix data type.
- support using just few TimeStamps fields.
- Add ForceRemove function.
- Add Trashed function.
- Add Restore function.
- Update Microsoft.EntityFrameworkCore package to version 7.0.10.