DKNet.EfCore.Extensions
9.0.3
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
<PackageReference Include="DKNet.EfCore.Extensions" Version="9.0.3" />
<PackageVersion Include="DKNet.EfCore.Extensions" Version="9.0.3" />
<PackageReference Include="DKNet.EfCore.Extensions" />
paket add DKNet.EfCore.Extensions --version 9.0.3
#r "nuget: DKNet.EfCore.Extensions, 9.0.3"
#:package DKNet.EfCore.Extensions@9.0.3
#addin nuget:?package=DKNet.EfCore.Extensions&version=9.0.3
#tool nuget:?package=DKNet.EfCore.Extensions&version=9.0.3
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.
- Establish a generic type of
IEntityTypeConfiguration
. For instance, it could be calledDefaultEntityTypeConfiguration
.
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
}
}
- 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()
{
}
}
- 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; }
}
- 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.
- 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.
}
}
- 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 | Versions 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. |
-
net9.0
- DKNet.EfCore.Abstractions (>= 9.0.3)
- DKNet.Fw.Extensions (>= 9.0.3)
- Meziantou.Analyzer (>= 2.0.202)
- Microsoft.EntityFrameworkCore (>= 9.0.6)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.6)
- System.ComponentModel.Annotations (>= 5.0.0)
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 |