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
<PackageReference Include="Wsu.Asis.EntityFrameworkCore.Extensions" Version="1.0.0.3" />
<PackageVersion Include="Wsu.Asis.EntityFrameworkCore.Extensions" Version="1.0.0.3" />
<PackageReference Include="Wsu.Asis.EntityFrameworkCore.Extensions" />
paket add Wsu.Asis.EntityFrameworkCore.Extensions --version 1.0.0.3
#r "nuget: Wsu.Asis.EntityFrameworkCore.Extensions, 1.0.0.3"
#:package Wsu.Asis.EntityFrameworkCore.Extensions@1.0.0.3
#addin nuget:?package=Wsu.Asis.EntityFrameworkCore.Extensions&version=1.0.0.3
#tool nuget:?package=Wsu.Asis.EntityFrameworkCore.Extensions&version=1.0.0.3
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 | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 7.0.20)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.20)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.