Toolbelt.EntityFrameworkCore.DecimalAttribute 5.0.1.5

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

DecimalAttribute for Entity Framework Core

NuGet Package unit tests

What's this?

This library for Entity Framework Core lets you set the precision and scale of decimal columns by annotating the properties of your entity types, like this.

[Decimal(10, 5)] // <-- This!
public decimal Height { get; set; }

The automated tests check that it works on SQL Server and PostgreSQL.

How to use?

  1. Add the Toolbelt.EntityFrameworkCore.DecimalAttribute package to your project.
> dotnet add package Toolbelt.EntityFrameworkCore.DecimalAttribute

Supported Versions

EF Core version Package version
v.3.1, 5, 6, 7, ...11 v.5.0.1 or later (recommended), v.5.0, v.3.1
v.3.0 v.5.0.1 or later (recommended), v.5.0, v.3.1, v.3.0
v.2.0, 2.1, 2.2 v.1.0.x
  1. Annotate your model with the [Decimal(precision, scale)] attribute. It lives in the Toolbelt.ComponentModel.DataAnnotations.Schema.V5 namespace.
using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;

public class Person
{
    public int Id { get; set; }

    [Decimal(10, 5)] // <- Here!
    public decimal Height { get; set; }
}
  1. [Important] Override the OnModelCreating() method of your DbContext class, and call the BuildDecimalColumnTypeFromAnnotations() extension method. It lives in the Toolbelt.ComponentModel.DataAnnotations namespace.
using Microsoft.EntityFrameworkCore;
using Toolbelt.ComponentModel.DataAnnotations;

public class MyDbContext : DbContext
{
    ...
    // Override "OnModelCreating", ...
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // .. and invoke "BuildDecimalColumnTypeFromAnnotations"!
        modelBuilder.BuildDecimalColumnTypeFromAnnotations();
    }
}

That's all!

The BuildDecimalColumnTypeFromAnnotations() extension method scans the DbContext with .NET reflection, finds the [Decimal] attributes, and sets the column types in the model.

As a result, the decimal columns in the database that EF Core creates have the precision and scale that you specified with the [Decimal] attribute.

Appendix

Upgrade an existing project

Follow these steps to upgrade an existing project from ver.3 or earlier to ver.5 or later of this package.

  1. Update this package to ver.5 or later.
PM> Update-Package Toolbelt.EntityFrameworkCore.DecimalAttribute
  1. Replace using Toolbelt.ComponentModel.DataAnnotations.Schema; with using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;.
...
// 👇 Remove this line...
// using Toolbelt.ComponentModel.DataAnnotations.Schema;

// 👇 Insert this line, instead.
using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;
...

If you want to use only "DecimalAttribute" without any dependencies...

If you want to use only the "DecimalAttribute" class without any dependencies, use the Toolbelt.EntityFrameworkCore.DecimalAttribute.Attribute NuGet package.

Release Notes

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 is compatible.  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.  net11.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
5.0.1.5 48 10/15/2026
5.0.1.4 38 10/15/2026
5.0.1.3 483 11/21/2025
5.0.1.2 1,480 11/13/2024
5.0.1.1 3,194 11/5/2023
5.0.1 4,738 11/11/2021
5.0.1-preview.1.0 372 6/30/2021
5.0.0 5,553 8/14/2020
3.1.0 738 2/5/2020
3.0.0 746 10/10/2019
1.0.1 973 7/26/2019
1.0.0 883 2/21/2019

v.5.0.1.5
- Update AnnotationBasedModelBuilder to