sql server - Can I map a decimal property to a float db column -


i'm writing .net web app on top of existing db app (sql server). original developer stored money in float columns instead of money or decimal. want use decimal inside pocos (especially because doing further operations on values after them).

unfortunately, cannot touch db schema. there way still use decimal in pocos , tell ef "it's ok, know decimal , float don't along. don't either. best."

with no special configuration, error:

the specified cast materialized 'system.double' type 'system.decimal' type not valid.

i tried using modelbuilder.entity(of mypocotype).property(function(x) x.moneyproperty).hascolumntype("float"), gets me error:

schema specified not valid. errors: (195,6) : error 0063: precision facet isn't allowed properties of type float. (195,6) : error 0063: scale facet isn't allowed properties of type float. 

something work:

public class mypocotype {     public float floatprop { get; set; }      [notmapped]     public decimal decimalprop     {                 {             return (decimal)floatprop;         }         set         {             floatprop = (float)value;         }     } } 

ef ignore decimal one, can use , it'll set underlying float. can add in own logic handling loss of precision if there's special want do, , might need catch cases value out of range of it's being converted (float has larger range, decimal has greater precision).

it's not perfect solution, if you're stuck floats it's not going better. float inherently little fuzzy, that's going trip somewhere.

if want complex, @ keeping decimal value internally , using various events happen @ save time (some logic in overridden savechanges(), or catching savingchanges event perhaps) convert float once, cut down on buildup of conversion errors.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -