DKNet.EfCore.Extensions 9.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package DKNet.EfCore.Extensions --version 9.0.3
                    
NuGet\Install-Package DKNet.EfCore.Extensions -Version 9.0.3
                    
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="DKNet.EfCore.Extensions" Version="9.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DKNet.EfCore.Extensions" Version="9.0.3" />
                    
Directory.Packages.props
<PackageReference Include="DKNet.EfCore.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DKNet.EfCore.Extensions --version 9.0.3
                    
#r "nuget: DKNet.EfCore.Extensions, 9.0.3"
                    
#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.
#:package DKNet.EfCore.Extensions@9.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DKNet.EfCore.Extensions&version=9.0.3
                    
Install as a Cake Addin
#tool nuget:?package=DKNet.EfCore.Extensions&version=9.0.3
                    
Install as a Cake Tool

DKNet.EfCore.Extensions

Nuget Package

PM> Install-Package DKNet.EfCore.Extensions

Overview

Entity Framework Core is a lightweight, extensible, cross-platform variation of the widely-used Entity Framework data access technology. However, to operationalize the Entity Framework, several things need to be defined and configured:

Automated EntityTypeConfiguration

When working with EfCore, for every new Entity you add, you need to define the configuration of IEntityTypeConfiguration and incorporate them into the OnConfiguring of the DbContext. This library provides a more automated, efficient way to accomplish this.

  1. Establish a generic type of IEntityTypeConfiguration. For instance, it could be called DefaultEntityTypeConfiguration.
internal class DefaultEntityTypeConfiguration<T> : IEntityTypeConfiguration<T> where T : BaseEntity
{
    public override void Configure(EntityTypeBuilder<T> builder)
    {
        // Configuration details that will apply to all Entities inherited from BaseEntity
    }
}
  1. Your Base Entity
//Base entity should inherit IEntity from DKNet.EfCore.Abstractions where the magic happens.
public abstract class BaseEntity : IEntity
{
    /// The ID property will be shared across all child classes
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}

    /// Base classes need a constructor to allow child classes to define a private constructor
    protected BaseEntity()
    {
    }
}
  1. Define the Entities
public class User: BaseEntity
{
    public string FullName => $"{FirstName} {LastName}";

    [Required]
    [MaxLength(256)]
    public string FirstName { get; set; }

    [Required]
    [MaxLength(256)]
    public string LastName { get; set; }
}
  1. Service Configuration
//Use this configuration if your Entities are in the same assembly as `YourDbContext`
service.AddDbContext<YourDbContext>(builder=>{
    builder.UseSqlServer(ConnectionString)
            //This is crucial
            .UseAutoConfigModel()
});

//Or use this configuration if your Entities are in a different assembly than `YourDbContext`
service.AddDbContext<YourDbContext>(builder=>{
    builder.UseSqlServer(ConnectionString)
            //This is crucial
            .UseAutoConfigModel(op => op.ScanFrom([YourAssempbly])
});

That's all. The User entity will automatically load using DefaultEntityTypeConfiguration at runtime. However, if a specific configuration needs to be defined for a particular entity, you can still define a dedicated configuration class for that entity. The library is intelligent enough to detect your configuration at runtime without any additional requirements.

  1. Define a dedicated configuration class for a specific entity.
internal class UserTypeConfiguration<T> : IEntityTypeConfiguration<User>
{
    public override void Configure(EntityTypeBuilder<User> builder)
    {
        // Define your configuration here.
    }
}
  1. Excluding an Entity from Automatic Type Configuration There might be instances where you need to exclude a specific entity from the automatic type configuration described above. In such cases, you can utilize the [IgnoreEntityMapper] attribute to achieve this. The following code snippet illustrates how to implement this:
[IgnoreEntityMapper]
public class OtherEntity: BaseEntity
{
    
}

This attribute allows you to retain control over your entity configurations, providing flexibility when certain entities require a different configuration approach.

Data Seeding Management

In EFCore, managing data seeding necessitates the definition of static data along with the entity configuration within the same EntityTypeConfiguration class. To make this process more manageable, this library offers an alternative way to handle data seeding. The library takes care of the complexities, allowing you to focus on defining your seeding strategy.

A distinct advantage of this approach is that it enables the consolidation of all data seeding configurations into separate classes and folders, significantly improving organization and maintainability.

Define a specific data seeding class

public class DefaultUserData : IDataSeedingConfiguration<User>
{
    public ICollection<User> Data => new[]
    {
        new User
        {
            Id = 1,
            FirstName = "Admin",
            LastName = "Account"
        }
    };
}

This approach allows for a more streamlined and organized way to manage data seeding, enhancing overall productivity and efficiency.

Enum-Based Static Data Seeding

As developers, we understand that when enum data gets stored in a database it is represented as an integer. This can pose a challenge for support teams in correctly identifying the meaning of these values. Consequently, we've introduced a new library that offsets this issue and offers a simple way to store enum classes as table data via the [StaticData("TableName")] attribute. Below is an example of how you can use this feature:

[StaticData(nameof(EnumStatus))]
public enum EnumStatus
{
    UnKnow,
    Active,
    InActive
}

With this implementation, each enum status is more easily identifiable for better clarity and maintenance.

Global Query Filters

Global query filters are a beneficial feature provided by EfCore that applies such filters automatically to any LINQ queries involving those Entity Types. EF Core also applies them to Entity Types, referenced indirectly through use of Include or navigation property. Some common applications of this feature are:

  • Soft-delete - An Entity Type defines an IsDeleted property.
  • Multi-tenancy - An Entity Type defines a TenantId property.

To use this feature, the suitable filters should be registered concurrently in the IEntityTypeConfiguration class. Similar to the Data Seeding Management process, this library includes an IGlobalModelBuilderRegister interface. It separates the management of global query filters from the Entity Type Configuration process. An example of global query filter usage is outlined below:

public class SampleQueryRegister : IGlobalModelBuilderRegister
{
     public void Apply(ModelBuilder? modelBuilder, DbContext context)
     {
         //Define your filter here
          modelBuilder.Entity<User>()
            .HasQueryFilter(x => !x.IsDelete);
     }
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on DKNet.EfCore.Extensions:

Package Downloads
DKNet.EfCore.Hooks

Package Description

DKNet.EfCore.Repos

Package Description

DKNet.EfCore.DataAuthorization

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.23 26 9/6/2025
9.0.22 129 9/3/2025
9.0.21 137 9/1/2025
9.0.20 162 7/15/2025
9.0.19 164 7/14/2025
9.0.18 162 7/14/2025
9.0.17 156 7/14/2025
9.0.16 138 7/11/2025
9.0.15 150 7/11/2025
9.0.14 139 7/11/2025
9.0.13 145 7/11/2025
9.0.12 162 7/8/2025
9.0.11 156 7/8/2025
9.0.10 157 7/7/2025
9.0.9 167 7/2/2025
9.0.8 158 7/2/2025
9.0.7 165 7/1/2025
9.0.6 161 6/30/2025
9.0.5 166 6/24/2025
9.0.4 166 6/24/2025
9.0.3 170 6/23/2025
9.0.2 166 6/23/2025
9.0.1 165 6/23/2025