Fluent API HasColumnType - Database Agnostic

2.3k Views Asked by At

We are currently using the following to set money column types in MS SQL Server.

entity.Property(e => e.UnitPrice).HasColumnType("smallmoney");

or

entity.Property(e => e.UnitPrice).HasColumnType("money");

What, if at all, is a more database agnostic way?

1

There are 1 best solutions below

2
On

Use Decimal instead of Money. Use Decimal in c# and in Db.
See also

modelBuilder.Entity<MetaDomain>().Property(d => d.xyz).HasPrecision(precision, scale);

decimals in SQL server docu

MSDN decimal which states

which makes it appropriate for financial and monetary calculations.

Surely Decimal is the most DB agnostic way of dealing with currency. You should choose precision/scale to match the currencies and accuracy required. And i would also suggest you always track the currency itself with the amount unless you know the solution never could or will apply beyond 1 currency.

For "monetary amounts" means catering 4 extra decimal places than the currency itself describes. Since industries like high volume retailers that manage items in fractions of cents. I had to demo 4 decimal places to a retailer in a presentation. It means catering for many different currencies and as more organizations do in house treasury, if means catering for AT LEAST 5 decimals. Im planning beyond that just in case. XDR special drawing rights