Skip to content

Instantly share code, notes, and snippets.

@wkrea
Forked from mombrea/Precision.cs
Created January 11, 2020 05:12
Show Gist options
  • Save wkrea/0107da447bc937fd7b0eabb560f4a2ad to your computer and use it in GitHub Desktop.
Save wkrea/0107da447bc937fd7b0eabb560f4a2ad to your computer and use it in GitHub Desktop.
.NET Data Annotation to define precision on decimal data types in EF6
using System;
using System.Data.Entity;
using System.Linq;
namespace My.Data.Annotations
{
/// <summary>
/// The Precision class allows us to decorate our Entity Models with a Precision attribute
/// to specify decimal precision values for the database column
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class Precision : Attribute
{
/// <summary>
/// The total number of digits to store, including decimals
/// </summary>
public byte precision { get; set; }
/// <summary>
/// The number of digits from the precision to be used for decimals
/// </summary>
public byte scale { get; set; }
/// <summary>
/// Define the precision and scale of a decimal data type
/// </summary>
/// <param name="precision">The total number of digits to store, including decimals</param>
/// <param name="scale">The number of digits from the precision to be used for decimals</param>
public Precision(byte precision, byte scale)
{
this.precision = precision;
this.scale = scale;
}
/// <summary>
/// Apply the precision to our data model for any property using this annotation
/// </summary>
/// <param name="modelBuilder"></param>
public static void ConfigureModelBuilder(DbModelBuilder modelBuilder)
{
modelBuilder.Properties().Where(x => x.GetCustomAttributes(false).OfType<Precision>().Any())
.Configure(c => c.HasPrecision(c.ClrPropertyInfo.GetCustomAttributes(false).OfType<Precision>().First()
.precision, c.ClrPropertyInfo.GetCustomAttributes(false).OfType<Precision>().First().scale));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment