0

We're developing a web application using ASP.Net MVC 5 C#, I'm having difficulties coming up with a nice solution for my problem.

We're accessing an api that has a few enums coming across as text (i.e. "yes", "no", "notfound", "na") on the model itself I'd like to have strongly typed enums that store in the database as an integer (save a little space I think) the issue comes when we're deserializing the response from the api. In the response it will comes across as text (see above) where as the enums will be expecting an integer.

How is this done? I really hate asking questions where I haven't tried anything but my web searches hasn't resulted in anything. If you needed any code please let me know and I'll update the question with the segments needed. As of right now the model has several strongly typed enums and the api is returning strings of the enum values. by the way The enum texts are the same as the return values.

In-case it makes any difference we're also using EF Code First 6

hjavaher
  • 2,589
  • 3
  • 30
  • 52
  • Does http://stackoverflow.com/questions/1526339/how-to-work-with-enums-in-entity-framework help you? – LosManos Jun 21 '14 at 21:41
  • @LosManos Thanks for that link. I already know that EF6 works with Enums. My problem is the deserialization of the api response. it doens't transmit int for the enums. it returns the enum text (Yes, No, NotFound, etc) – hjavaher Jun 21 '14 at 22:11

1 Answers1

0

You can use Enum.TryParse to convert the string to its enum value

public enum MyEnum
{
  NA
  Yes,
  No
}

then

MyEnum value = MyEnum.NA;
Enum.TryParse<MyEnum>(theTextValue, out value );