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
<PackageReference Include="Toolbelt.EntityFrameworkCore.DecimalAttribute" Version="5.0.1.5" />
<PackageVersion Include="Toolbelt.EntityFrameworkCore.DecimalAttribute" Version="5.0.1.5" />
<PackageReference Include="Toolbelt.EntityFrameworkCore.DecimalAttribute" />
paket add Toolbelt.EntityFrameworkCore.DecimalAttribute --version 5.0.1.5
#r "nuget: Toolbelt.EntityFrameworkCore.DecimalAttribute, 5.0.1.5"
#:package Toolbelt.EntityFrameworkCore.DecimalAttribute@5.0.1.5
#addin nuget:?package=Toolbelt.EntityFrameworkCore.DecimalAttribute&version=5.0.1.5
#tool nuget:?package=Toolbelt.EntityFrameworkCore.DecimalAttribute&version=5.0.1.5
DecimalAttribute for Entity Framework Core
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?
- Add the
Toolbelt.EntityFrameworkCore.DecimalAttributepackage 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 |
- Annotate your model with the
[Decimal(precision, scale)]attribute. It lives in theToolbelt.ComponentModel.DataAnnotations.Schema.V5namespace.
using Toolbelt.ComponentModel.DataAnnotations.Schema.V5;
public class Person
{
public int Id { get; set; }
[Decimal(10, 5)] // <- Here!
public decimal Height { get; set; }
}
- [Important] Override the
OnModelCreating()method of your DbContext class, and call theBuildDecimalColumnTypeFromAnnotations()extension method. It lives in theToolbelt.ComponentModel.DataAnnotationsnamespace.
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.
- Update this package to ver.5 or later.
PM> Update-Package Toolbelt.EntityFrameworkCore.DecimalAttribute
- Replace
using Toolbelt.ComponentModel.DataAnnotations.Schema;withusing 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
- Toolbelt.EntityFrameworkCore.DecimalAttribute - Release Notes
- Toolbelt.EntityFrameworkCore.DecimalAttribute.Attribute - Release Notes
License
| Product | Versions 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. |
-
.NETStandard 2.0
-
.NETStandard 2.1
-
net10.0
-
net11.0
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