Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation 30000 No Type Specified for the Decimal Column

What's the best way of specifying a decimal precision without using attributes. I just need to set it in one place for all decimal's in my Data.Models. Its tedious specifying attributes for every decimal.

public class Customer
{
    public int customerId { get; set; }

    [Column(TypeName = "decimal(18,2)")]
    public decimal AvailableAmount { get; set; }
}
like image 604
Sydney_dev Avatar asked Jun 24 '20 08:06

Sydney_dev


3 Answers

I experienced this issu using ASP.NET CORE 5. I added the folowing code to the OnModelcreating method in the DbContext.

protected override void OnModelCreating(ModelBuilder modelBuilder)// Crée la migration
    {
        modelBuilder.Entity<MyEntity>().Property(p => p.Prix).HasColumnType("decimal(18,4)");

    }

And all started working fine.

like image 116
DOUMBIA Mamadou Avatar answered Nov 17 '22 11:11

DOUMBIA Mamadou


Add following to the OnModelCreating method in the dbcontext:

 protected override void OnModelCreating(ModelBuilder builder)
        {
          
            foreach (var property in builder.Model.GetEntityTypes()
                .SelectMany(t => t.GetProperties())
                .Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
            {
              
                property.Relational().ColumnType = "decimal(18,2)";

              
            }
        }
like image 30
vahid tajari Avatar answered Nov 17 '22 11:11

vahid tajari


For those who are struggling with the same issue on EntityFrameworkCore 6.0, this will do it:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var decimalProps = modelBuilder.Model
    .GetEntityTypes()
    .SelectMany(t => t.GetProperties())
    .Where(p => (System.Nullable.GetUnderlyingType(p.ClrType) ?? p.ClrType) == typeof(decimal));

    foreach (var property in decimalProps)
    {
        property.SetPrecision(18);
        property.SetScale(2);
    }
}
like image 2
Bigabdoul Avatar answered Nov 17 '22 11:11

Bigabdoul