Question

I'm working with LINQ for the first time and wanted to get the Mapping to work when I have a money type in SQL, but my domain object property is of type double. How can I express this in the XML file, or in code so that the mapping does not throw the usual "invalid cast" exception?

Was it helpful?

Solution

In the DBML XML file, you can set the Expression attribute of a Column element to something like this:

 <Column Name="Table1.Amount" DbType="smallint" Type="System.Int32" 
         Expression="CAST(Table1.Amount as int)" />

OTHER TIPS

Slightly off topic, but this is something everyone should know when working with the Money type in SQLServer.

You don't want to use a double, you want to use a decimal.

A Double is a long float, and floating point arithmetic should never be used for financial calculations.

Think of this, the fractions 1/3, 1/3 and 1/3 equal 1. However, when expressed as a double ie:

.3333 + .3333 + .3333 = .9999 not 1.

You may think that losing a thousandth of a cent is trivial, but its not when your working with someone elses money.

Use System.Decimal.

pretty sure Money maps to System.Decimal

Check here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top