Enigmatry.Entry.SmartEnums.EntityFramework 9.1.1-preview.3

This is a prerelease version of Enigmatry.Entry.SmartEnums.EntityFramework.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Enigmatry.Entry.SmartEnums.EntityFramework --version 9.1.1-preview.3
                    
NuGet\Install-Package Enigmatry.Entry.SmartEnums.EntityFramework -Version 9.1.1-preview.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="Enigmatry.Entry.SmartEnums.EntityFramework" Version="9.1.1-preview.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enigmatry.Entry.SmartEnums.EntityFramework" Version="9.1.1-preview.3" />
                    
Directory.Packages.props
<PackageReference Include="Enigmatry.Entry.SmartEnums.EntityFramework" />
                    
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 Enigmatry.Entry.SmartEnums.EntityFramework --version 9.1.1-preview.3
                    
#r "nuget: Enigmatry.Entry.SmartEnums.EntityFramework, 9.1.1-preview.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 Enigmatry.Entry.SmartEnums.EntityFramework@9.1.1-preview.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=Enigmatry.Entry.SmartEnums.EntityFramework&version=9.1.1-preview.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Enigmatry.Entry.SmartEnums.EntityFramework&version=9.1.1-preview.3&prerelease
                    
Install as a Cake Tool

Smart Enums EntityFramework Integration

This library provides integration between Smart Enums and Entity Framework Core, enabling you to use Smart Enum types as properties in your entity models.

Intended Usage

Use this library to seamlessly store and retrieve Smart Enum values when using Entity Framework Core as your ORM.

Installation

Add the package to your project:

dotnet add package Enigmatry.Entry.SmartEnums.EntityFramework

Usage Example

Configure your DbContext to use Smart Enum converters:

using Ardalis.SmartEnum;
using Enigmatry.Entry.SmartEnums.Entities;
using Enigmatry.Entry.SmartEnums.EntityFramework;
using Microsoft.EntityFrameworkCore;

// Example SmartEnum
public class ProductCategory : SmartEnum<ProductCategory>
{
    public static readonly ProductCategory Electronics = new(1, nameof(Electronics));
    public static readonly ProductCategory Clothing = new(2, nameof(Clothing));
    public static readonly ProductCategory Books = new(3, nameof(Books));
    public static readonly ProductCategory HomeGoods = new(4, nameof(HomeGoods));

    private ProductCategory(int id, string name) : base(id, name) { }
}

// Entity with SmartEnum property
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ProductCategory Category { get; set; }
}

// Entity that uses SmartEnum as primary key
public class CategoryInfo : EntityWithEnumId<ProductCategory>
{
    public string Description { get; set; }
}

// Configuration for the CategoryInfo entity
public class CategoryInfoConfiguration : EntityWithEnumIdConfiguration<CategoryInfo, ProductCategory>
{
    public CategoryInfoConfiguration() : base(nameMaxLength: 50) { }
}

public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<CategoryInfo> CategoryInfos { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Configure all SmartEnums in the application
        modelBuilder.EntryConfigureSmartEnums();
        
        // Option 1: Configure a specific SmartEnum property
        modelBuilder.Entity<Product>()
            .Property(p => p.Category)
            .HasSmartEnumConversion();
            
        // Option 2: Apply the entity configuration
        modelBuilder.ApplyConfiguration(new CategoryInfoConfiguration());
    }
}

Using the newly configured model:

// Create and save an entity with a SmartEnum property
using (var context = new ApplicationDbContext())
{
    var product = new Product
    { 
        Name = "Laptop",
        Category = ProductCategory.Electronics
    };
    
    context.Products.Add(product);
    await context.SaveChangesAsync();
}

// Query entities by SmartEnum value
using (var context = new ApplicationDbContext())
{
    var electronicsProducts = await context.Products
        .Where(p => p.Category == ProductCategory.Electronics)
        .ToListAsync();
}
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

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
9.1.1-preview.4 91 6/27/2025
9.1.1-preview.3 115 6/4/2025
9.1.0 142 6/3/2025
9.0.1-preview.8 122 5/26/2025
9.0.1-preview.7 214 5/13/2025
9.0.1-preview.6 207 5/9/2025
9.0.1-preview.5 148 5/7/2025
9.0.1-preview.4 120 4/30/2025
9.0.1-preview.2 129 4/1/2025
9.0.0 157 2/26/2025
8.1.1-preview.3 122 5/7/2025
8.1.1-preview.1 129 4/1/2025
8.1.0 290 2/19/2025
8.0.1-preview.4 75 2/7/2025
8.0.1-preview.2 61 1/15/2025
8.0.0 733 11/27/2024
3.4.6-preview.10 70 11/27/2024
3.4.3 406 10/22/2024
3.4.2 452 10/11/2024
3.4.1 118 10/9/2024
3.4.0 116 10/9/2024
3.3.2 239 8/28/2024
3.3.2-preview.7 92 8/27/2024