Wsu.Asis.EntityFrameworkCore.Extensions 1.0.0.3

dotnet add package Wsu.Asis.EntityFrameworkCore.Extensions --version 1.0.0.3
                    
NuGet\Install-Package Wsu.Asis.EntityFrameworkCore.Extensions -Version 1.0.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="Wsu.Asis.EntityFrameworkCore.Extensions" Version="1.0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wsu.Asis.EntityFrameworkCore.Extensions" Version="1.0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Wsu.Asis.EntityFrameworkCore.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 Wsu.Asis.EntityFrameworkCore.Extensions --version 1.0.0.3
                    
#r "nuget: Wsu.Asis.EntityFrameworkCore.Extensions, 1.0.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 Wsu.Asis.EntityFrameworkCore.Extensions@1.0.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=Wsu.Asis.EntityFrameworkCore.Extensions&version=1.0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Wsu.Asis.EntityFrameworkCore.Extensions&version=1.0.0.3
                    
Install as a Cake Tool

WSU ASIS Entity Framework Core Extensions

This package contains a set of tools to make creating databases easier.

EnumEntity

This is a generic class that makes it easy to add tables for Enums without having to create a specific entity class for every Enum. This will create an entity with this structure:

public class EnumEntity<T> where T : Enum
{
	[Key]
	public required T Id { get; set; }
	[Required, MaxLength(255)]
	public required string Name { get; set; }
	public string? Description { get; set; }
}

The Name is either the enum name or the name provided in a DisplayAttribute. The description is pulled from the description in a DisplayAttribute.

Example

public enum MyEnum
{
	[Display(Name = "Value 1", Description = "A description for value 1")]
	Value1 = 1,
	[Display(Name = "Value 2")]
	Value2 = 2
}

public class MyEntity
{
	[Key]
	public int MyEntityId { get; set; }
	[ForeignKey(nameof(MyEnumDetails))]
	public MyEnum MyEnum { get; set; }

	public EnumEntity<MyEnum>? MyEnumDetails { get; set; }
}

public class MyDbContext : DbContext
{
	public DbSet<EnumEntity<MyEnum>> MyEnums { get; set; }
	public DbSet<MyEntity> MyEntities { get; set; }

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.ConfigureEnumEntities();
		modelBuilder.SeedEnumEntities();
	}

	protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
	{
		// Store enums as ints
		configurationBuilder.Properties<Enum>().HaveConversion<int>();
	}
}

SoftDelete

This enables automatically setting a RecordCreatedDate when an entity is added, a RecordModifiedDate when an entity is modified, and a RecordInactiveDate instead of actually deleting the record. This is done by inheriting from SoftDeleteDbContext instead of the standard DbContext, which overrides .SaveChanges() and .SaveChangesAsync() to set these values when called.

public class MyEntity : SoftDelete
{
	[Key]
	public int MyEntityId { get; set; }
	public string Name { get; set; }
}

public class MyDbContext : SoftDeleteDbContext
{
	public DbSet<MyEntity> MyEntities { get; set; }

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		this.ConfigureSoftDeleteEntities(modelBuilder);
	}
}

You can then add, update, and remove entities like normal. For example, myDb.MyEntities.Remove(anEntity) will not delete the entity from the database. Since MyEntity extends SoftDelete, theRecordInactiveDate will be set to DateTime.Now instead.

Parameterized Order and OrderBy

LINQ provides functions likeOrderBy and OrderByDescending. However, you have to select which sort direction to use at build-time or use if statements that interrupt LINQ's fluent syntax. This library provides two extension methods that allows you to provide the sort direction as a parameter. This makes it easier to, for example, make the sort direction an option the user can choose.

var sortedThings = things
	.OrderBy(t => t.ThingId, IOrderedQueryableDirection.Ascending);

var sortedNumbers = numbers
	.Order(IOrderedQueryableDirection.Descending);

Note that Entity Framework is not able to translate this, so these can only be used at the top level of a query:

// Works
var things = await myDb.Things
	.OrderBy(t => t.ThingId, IOrderedQueryableDirection.Ascending)
	.ToListAsync();

// Fails
var things = await myDb.Things
	.Select(t => new
	{
		t.ThingId,
		SubThings = t.SubThings
			.OrderBy(s => s.SubThingId, IOrderedQueryableDirection.Ascending)
			.ToList()
	})
	.ToListAsync();

This is because in the first example the OrderBy is actually called and it replaces it with either a standard OrderBy or OrderByDescending. In the second example, the select expression is never actually called - Entity Framework uses reflection on it to translate it into a SQL query. Since this function is unknown to Entity Framework, it can't translate it to SQL and throws an exception.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.3 206 10/20/2025
1.0.0.2 193 10/20/2025
1.0.0.1 238 8/14/2024
1.0.0 181 8/14/2024