4

I would like to something like this:

public enum States
{
    California,
    [Description("New Mexico")]
    NewMexico,
    [Description("New York")]
    NewYork,
    [Description("South Carolina")]
    SouthCarolina,
    Tennessee,
    Washington
}

So that when I'm making a Drop Down List using .NET MVC I can use spaces in my names. If I used Code First then I could do this but I am using Database First and the .edmx designer doesn't allow attributes on the generated states (as far as I know!). Any ideas on how I can do this? I am using .NET 4.5 and EF 5 beta.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • I assume you are pulling the states from a table? Could you please post the table structure? – Maess Jun 19 '12 at 12:45
  • Actually no, you can define the Enum and it's states in the designer without having to have a related table. – mikeydelamonde Jun 19 '12 at 12:58
  • Ok, so you are doing a Data First / Model First hybrid? – Maess Jun 19 '12 at 13:01
  • Thanks for looking at this! I am using the EF DbContext Generator to make my Classes from an EDMX file which I have generated from the DB. – mikeydelamonde Jun 19 '12 at 13:06
  • 1
    I worked it out! You create the Enum in a class. Then you add the Enum to the field in the designer, give it the same name as the one you wrote. Then tick the box that says "Reference External Type" and give it the Namespace+type. Then it will reference yours rather than generating code. – mikeydelamonde Jun 19 '12 at 14:38
  • @mikeydelamonde, how have you done it exactly? I tried the same but it is still generating the enum code, even by using the "Reference External Type" fields... – Konamiman Aug 03 '12 at 08:01

1 Answers1

1

It's only possible to use external types for enums if you're using DbContext based T4 templates. It's not supported for entities that derive from EntityObject or use ObjectContext directly.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742