Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have lovely descriptive enums in my code, but when persisted, records using those are persisted with their integer values. I would prefer the string value.
Posted
Comments
Kornfeld Eliyahu Peter 21-Sep-14 4:06am    
You do it all wrong! Use the integer values! no one should read you DB as-is, but via the UI. Use the UI to translate the integer back to it's string representation...
Brady Kelly 21-Sep-14 4:30am    
Yes Mom, I know the way it should be done, but that involves keeping my code enum in sync with my DB int values, and I'm not generating the database, it's fixed, so sometimes we overlook unimportant naughtinesses.
Kornfeld Eliyahu Peter 21-Sep-14 4:42am    
Use look-up tables inside your DB. A simple FK will take care for synchronization...
Brady Kelly 21-Sep-14 5:17am    
I meant the synchronisation between the coded enum and the lookup table. To be reliable, I would have to rewrite the table from the enum every time my application starts. Hell, I'll just do it by hand for now.

It seems not directly since enums can only be mapped to Byte, Int16, Int32, Int64 or SByte, but you can read this : https://entityframework.codeplex.com/discussions/451445[^]
 
Share this answer
 
Comments
Brady Kelly 21-Sep-14 6:11am    
Seems a sort of workable solution is to just change the FK between Ids to between the Texts. Tidier than a long check constraint.
Mehdi Gholam 21-Sep-14 6:21am    
You could create a string property and have that be the enum "text" and not use the enum for storage.
You can, in a number of ways. Please see my articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

Your follow-up questions will be welcome.

Good luck.
—SA
 
Share this answer
 
I am doing that now. My worry there is it's harder to constrain the values to the enun names. I have to do POCO side validation.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Sep-14 20:49pm    
Not an answer. Such posts are considered as abuse. Instead, please use comments and/or "Improve question" above.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900