Hopefully this is an easy question. I have a legacy DB2 database on an AS/400 where fields like Zip Code are stored as NUMERIC. When mapping them in NHibernate they are treated as integers and the leading zeros are lost. Typically I would use
SEL开发者_如何转开发ECT DIGITS(field) FROM TABLE
to preserve leading zeros but its my understanding that if I go creating formulas to correct formatting I force the field to become read only.
What is the correct way to map the NUMERIC type (with its leading digits) to a string type and back again?
You are trying to deal with it at the wrong level.
Leading zeroes are a display concern; just use "00000"
as your format string.
Diego is right, but if you really need to represent the zip codes as strings in your domain you could implement your own IUserType and use it in your mappings. However, I'm not sure that you'll be able to handle getting the leading zeroes into the numeric database column (if this is something you need). I think it would be easy enough if you stored your zip code as '392' in the database and then presented it as '00392' though.
For a quick overiew of what's involved in creating an IUserType take a look at this blog post: Mapping Strings to Booleans using NHibernate's IUserType
精彩评论