Toolbelt.EntityFrameworkCore.DecimalAttribute 3.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Toolbelt.EntityFrameworkCore.DecimalAttribute --version 3.1.0                
NuGet\Install-Package Toolbelt.EntityFrameworkCore.DecimalAttribute -Version 3.1.0                
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="3.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.EntityFrameworkCore.DecimalAttribute --version 3.1.0                
#r "nuget: Toolbelt.EntityFrameworkCore.DecimalAttribute, 3.1.0"                
#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.
// Install Toolbelt.EntityFrameworkCore.DecimalAttribute as a Cake Addin
#addin nuget:?package=Toolbelt.EntityFrameworkCore.DecimalAttribute&version=3.1.0

// Install Toolbelt.EntityFrameworkCore.DecimalAttribute as a Cake Tool
#tool nuget:?package=Toolbelt.EntityFrameworkCore.DecimalAttribute&version=3.1.0                

DecimalAttribute for EntityFramework Core

NuGet Package

What's this?

This class library for Entity Framework Core on .NET Core/.NET Framework provides the ability for you to control the precision and scale of decimal column type, by annotating properties of entity types, like this.

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

How to use?

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

Supported Versions

EF Core version This package version
v.3.1 v.3.1
v.3.0 v.3.0, v.3.1
v.2.0, 2.1, 2.2 v.1.0.x
  1. Annotate your model with [Decimal(precision, scale)] attribute that lives in Toolbelt.ComponentModel.DataAnnotations.Schema namespace.
using Toolbelt.ComponentModel.DataAnnotations.Schema;

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

    [Decimal(10, 5)] // <- Here!
    public decimal Height { get; set; }
}
  1. [Important] Override OnModelCreating() method of your DbContext class, and call BuildDecimalColumnTypeFromAnnotations() extension method which lives in 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!

BuildDecimalColumnTypeFromAnnotations() extension method scans the DbContext with .NET Reflection technology, and detects [Decimal] attributes, then build models related to decimal column type.

After doing that, decimal type columns in the database which created by EF Core, is configured with precisions and scales that are specified by [Decimal] attribute.

Appendix

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

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

Release Note

  • v.3.1.0
    • Supports EntityFramework Core v.3.1.0
    • Revert back to .NET Standard 2.0
  • v.3.0.0 - BREAKING CHANGE: supports EntityFramework Core v.3.0
  • v.1.0.1
    • Fix: Doesn't work with owned types on EFCore v.2.1, v.2.2.
    • Fix: Doesn't work with nested owned types.
  • v.1.0.0 - 1st release.

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. 
.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.2 79 11/13/2024
5.0.1.1 1,461 11/5/2023
5.0.1 3,457 11/11/2021
5.0.1-preview.1.0 190 6/30/2021
5.0.0 5,285 8/14/2020
3.1.0 508 2/5/2020
3.0.0 481 10/10/2019
1.0.1 707 7/26/2019
1.0.0 587 2/21/2019

v.3.1.0
- Supports EntityFramework Core v.3.1.0
- Revert back to .NET Standard 2.0
v.3.0.0
- BREAKING CHANGE: supports EntityFramework Core v.3.0
v.1.0.1
- Fix: Doesn't work with owned types on EFCore v.2.1, v.2.2.
- Fix: Doesn't work with nested owned types.
v.1.0.0
- 1st release.