Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set display attributes to an E.F. generated enumeration?

I am just getting into EF. For the past 2 years I have been coding database code by hand and finally got fed up with it. I am having one issue when it comes to using enums with E.F. All my previously coded enums used display attributes so that when bound in xaml to a data form or any other control, would display friendly names instead of code names. EX display "Light Brown" instead of "LightBrown or light_brown" etc. All the enum designer allows is to set values and names. Is there a way to get these auto enums to show a different name when bound to?

[Display(Name="Light Brown")]
Light_Brown,

Edit I am not looking how to convert enum names into friendlier names. I already know how to do that. My issues is accessing the auto generated code for enumerations that are automatically generated using the entity framework designer. It seems there is a way to reference an external code file so I am going to look into this.

like image 698
Adrian Avatar asked Jan 26 '26 19:01

Adrian


2 Answers

Found a question and answer here on stack. You can circumvent this issue by using the "Reference External Type" option in the designer. This is only a solution in the event you are using the database first design of Entity Framework and not the model first design.

Entity Framework 5 Enums with Description attribute using Database First

The author answered his own question under the comments of his question.

like image 157
Adrian Avatar answered Jan 28 '26 09:01

Adrian


Try using

[Description("Light Brown")]
Light_Brown,

Check out this answer you might need to add a converter.

like image 21
sergioadh Avatar answered Jan 28 '26 08:01

sergioadh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!