I am working with an existing [poorly designed] database which stores a dollar amount as an integer (ie. $10.99 == 1099) in the database. I want my entity class to treat this value as a decimal so that consuming code doesn’t have to do the translation.
Is there a way to convert the value to and from integer to decimal and then back using Data Annotations or Fluent configuration?
In the meantime I am using a hack which essentially “wraps” the integer property with a decimal property that handles the translation.
[Column("intTotal")]
public virtual int TotalAsInt { get; set; }
[NotMapped]
public virtual decimal Total
{
get { return (decimal) TotalAsInt/100; }
set { TotalAsInt = (int)(value*100); }
}
Not as far as I know. Your solution looks good.
You could hide
TotalAsIntby making it aprivateproperty, but then you couldn’t use it inIQueryablemethods likeWhereorSelect.So, I would go with your solution until you can fix the database.