0

I have an enum to enumerate shipment type with "O" representing outbound and "I" meaning Inbound. Which of the below is more appropriate and why? with DataContract and EnumMember

[DataContract]
public enum ShipmentType
{
    [EnumMember(Value = "I")] Inbound,
    [EnumMember(Value = "O")] Outbound,
}

with description

public enum ShipmentType
{
    [Description("I")] Inbound,

    [Description("O")] Outbound,
}

Context: ShipmentType in DB is stored as "I" or "O". But in code I would like to use them as an enum. But while storing back to DB I still want to get the values "I" or "O". There are extension methods for getting EnumMember and Description value from the enum.

Does using one provide any advantage over the other?

  • 2
    More appropriate for *what*? These attributes were created by different teams at different times to serve different purposes. `EnumMember` isn't going to be considered by things that look for `Description`, and `Description` isn't going to affect data contract serialization. – Jeroen Mostert Sep 14 '22 at 13:40
  • It depends! What is the purpose of adding an attribute here? What are you trying to achieve? – phuzi Sep 14 '22 at 13:44
  • Given the presence of `[DataContract]` I suspect you want `EnumMember` to control (de)serialization. – phuzi Sep 14 '22 at 13:49
  • By DB do you mean ef core? in which case you want a value converter. https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations – Jeremy Lakeman Sep 15 '22 at 05:14
  • using ef on .net472 – Harish Ninge Gowda Sep 15 '22 at 07:24

0 Answers0