Anyname Donotcare Anyname Donotcare - 1 year ago 89
C# Question

How to change the precision and the scale of decimal globally through code first?

According to this answer:Decimal precision and scale in EF Code First

modelBuilder.Entity<Class>().Property(object =>, 10);

I can change the decimal precision and scale for specific property in specific entity .

But i wonder how to change it globally , i mean change all the decimal attributes to specific precision and scale because i have tons of them in different entities.

Answer Source

You can use DbModelBuilder.Properties method:

Begins configuration of a lightweight convention that applies to all primitive properties of the specified type in the model.

like this:

modelBuilder.Properties<decimal>().Configure(p => p.HasPrecision(12, 10));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download