'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; }
}
Solution 1:[1]
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.
Solution 2:[2]
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(decimal));
foreach (var property in properties)
{
modelBuilder.Entity(entityType.Name).Property(property.Name).HasColumnType("decimal(18,2)");}
http://jameschambers.com/2019/06/No-Type-Was-Specified-for-the-Decimal-Column/
Solution 3:[3]
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DOUMBIA Mamadou |
| Solution 2 | |
| Solution 3 |
