EF4 mapping nullable decimal column to non-nullable value property
I am using POCOs (no proxies) with EF4.
In the database, I have this nullable decimal column:
<Property Name="AMOUNT" Type="decimal" Precision="12" Scale="2" />开发者_如何学编程;
On my POCO, I have this non-nullable value property:
public decimal Amount { get; set; }
If the value in the database is null
, I want the property to be set to 0
. How can I achieve this? I would prefer not to have a Nullable
property here, otherwise I have to pollute my business logic with GetValueOrDefault()
code.
[This question seemed like what I was asking, but I'm not sure it was really answered.]
Unfortunately you must use nullable decimal in your POCO because EF doesn't provide any simple type mapping / type converters where you could place your transformation logic. Types must be same to make it work.
In case of EDMX there is one possible ugly workaround. You can map your column to non public property and expose another public property which will be not mapped (in your partial part of POCO class) and you will have your conversion logic in its getter and setter. Here is described how you can change visibility of property.
精彩评论