51

How can I do this :

private decimal _SnachCount;
[Required]
[DataType("decimal(16 ,3)")]
public decimal SnachCount
{
    get { return _SnachCount; }
    set { _SnachCount = value; }
}

private decimal _MinimumStock;
[Required]
[DataType("decimal(16 ,3)")]
public decimal MinimumStock
{
    get { return _MinimumStock; }
    set { _MinimumStock = value; }
}

private decimal _MaximumStock;
[Required]
[DataType("decimal(16 ,3)")]
public decimal MaximumStock
{
    get { return _MaximumStock; }
    set { _MaximumStock = value; }
}

After generating the database by this part of my model , these three columns type are decimal(18,2),why? what is this code error? how can i do that ?

James Hill
  • 60,353
  • 20
  • 145
  • 161
Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66

5 Answers5

82

The DataType Attribute is a Validation Attribute. You need to do that using the ModelBuilder.

public class MyContext : DbContext
{
    public DbSet<MyClass> MyClass;
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyClass>().Property(x => x.SnachCount).HasPrecision(16, 3);
        modelBuilder.Entity<MyClass>().Property(x => x.MinimumStock).HasPrecision(16, 3);
        modelBuilder.Entity<MyClass>().Property(x => x.MaximumStock).HasPrecision(16, 3);
    }
}
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • 1
    the second one does not work right , any way ,thank you , you helped me and your answer accepted, please omit the second idea in your answer,thanks – Ali Foroughi Jan 27 '12 at 12:23
  • 1
    Additional note: after you override the `OnModelCreating` method (as shown above), if you then run the `add-migration` command in the Package Manager Console, it will recognize your new code and will add a migration to correctly alter the columns. – Ross Brasseaux May 17 '17 at 18:55
  • Is this now also possible to do through data annotations using the column attribute like [Column(TypeName = "decimal(16,3)")] ? – Preza8 May 14 '19 at 09:46
  • modelBuilder.Entity().Property(x => x.X).HasColumnType("decimal(16,3)") will works fine with the EF Core 3.1.3. – Avinash May 18 '20 at 16:22
  • Yeah, because it would be way too easy if there was a way to do it using data annotations. – Jonathan Wood May 21 '20 at 19:30
  • This is what i wanted !, yes when i add migration after placing this code i got the change recognized, i do not have to write any plain SQL – Adel Mourad Jun 09 '22 at 13:24
35

You can modify all decimal propreties in database. In your DBContext in method OnModelCreating add line:

modelBuilder.Properties<decimal>().Configure(c => c.HasPrecision(18, 3));
Robert
  • 779
  • 6
  • 13
  • 1
    Saved my day!! So long was I trying around in the database tables but this is the solution. Thanks a lot!! – Tillito Aug 02 '17 at 17:10
22

This is copied from the answer I posted to the same question over here; https://stackoverflow.com/a/15386883/1186032.


I had a nice time creating an Custom Attribute for this:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
    public DecimalPrecisionAttribute(byte precision, byte scale)
    {
        Precision = precision;
        Scale = scale;

    }

    public byte Precision { get; set; }
    public byte Scale { get; set; }

}

using it like this

[DecimalPrecision(20,10)]
public Nullable<decimal> DeliveryPrice { get; set; }

and the magic happens at model creation with some reflection

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
    foreach (Type classType in from t in Assembly.GetAssembly(typeof(DecimalPrecisionAttribute)).GetTypes()
                                   where t.IsClass && t.Namespace == "YOURMODELNAMESPACE"
                                   select t)
     {
         foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null).Select(
                p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
         {

             var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
             ParameterExpression param = ParameterExpression.Parameter(classType, "c");
             Expression property = Expression.Property(param, propAttr.prop.Name);
             LambdaExpression lambdaExpression = Expression.Lambda(property, true,
                                                                      new ParameterExpression[]
                                                                          {param});
             DecimalPropertyConfiguration decimalConfig;
             if (propAttr.prop.PropertyType.IsGenericType && propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }
             else
             {
                 MethodInfo methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
                 decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
             }

             decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
        }
    }
}

the first part is to get all classes in the model (my custom attribute is defined in that assembly so i used that to get the assembly with the model)

the second foreach gets all properties in that class with the custom attribute, and the attribute itself so i can get the precision and scale data

after that i have to call

modelBuilder.Entity<MODEL_CLASS>().Property(c=> c.PROPERTY_NAME).HasPrecision(PRECITION,SCALE);

so i call the modelBuilder.Entity() by reflection and store it in the entityConfig variable then i build the "c => c.PROPERTY_NAME" lambda expression

After that, if the decimal is nullable i call the

Property(Expression<Func<TStructuralType, decimal?>> propertyExpression) 

method (i call this by the position in the array, it's not ideal i know, any help will be much appreciated)

and if it's not nullable i call the

Property(Expression<Func<TStructuralType, decimal>> propertyExpression)

method.

Having the DecimalPropertyConfiguration i call the HasPrecision method.

Community
  • 1
  • 1
KinSlayerUY
  • 1,903
  • 17
  • 22
  • 1
    This really should be part of EF ... shame really, its an elegant solution, but it will make a mess of OnModelCreation – War Nov 20 '15 at 10:53
  • https://github.com/richardlawley/EntityFrameworkAttributeConfig by http://stackoverflow.com/users/163495/richard helps make it much cleaner you can see his comment in my answer here: http://stackoverflow.com/questions/3504660/decimal-precision-and-scale-in-ef-code-first/15386883#15386883 – KinSlayerUY Dec 17 '15 at 19:05
13

So, what I got working for me is this:

public class RestaurantItemEntity : BaseEntity
{
    [Column(TypeName = "VARCHAR(128)")]
    [StringLength(128)]
    [Required]
    public string Name { get; set; }


    [Column(TypeName = "VARCHAR(1024)")]
    [StringLength(1024)]
    public string Description { get; set; }


    [Column(TypeName = "decimal(16,2)")]
    [Required]
    public decimal Price { get; set; }


    [Required]
    public RestaurantEntity Restaurant { get; set; }
}

This is EF Code first for .NET core.

Vulovic Vukasin
  • 1,540
  • 2
  • 21
  • 30
  • 1
    Just FYI, EF 6 will give a runtime exception if you include precision in the value for TypeName property of the ColumnAttribute. EF Core, on the other hand, demands it if you use that property. – esmoore68 May 12 '20 at 19:57
2

You can also set the precision of decimals using the code-first model mapping approach like this:

public class MyEntityMapping : EntityTypeConfiguration<MyEntity>
{
    public MyEntityMapping()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).IsRequired();
        // .HasPrecision(precision, scale)
        // 'precision' = total number of digits stored,
        // regardless of where the decimal point falls 
        // 'scale' = number of decimal places stored
        Property(x => x.DecimalItem).IsRequired().HasPrecision(16, 6);
    }
}
RickL
  • 3,318
  • 10
  • 38
  • 39