When using DataContractSerializer to write a DataContract with a member like:
[DataMember]
public PropertyId PropId { get; set; }
It reports a serialization error:
SerializationException: Enum value '8493' is invalid for type 'EnumType' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.
Yes, that particular enum value does not exist within the C# Enum definition because the PropertyId's are dynamic and may be added to the DB (but not deleted) between recompiles.
Since we do not wish to require a recompile of the code just because a property was added, is there any workaround for this type of issue besides typecasting and serializing as an Int32 (that's where we started from but moved to improve type safety/readability).
We would like to use this same concept to extend type safety to even more "generic" ID's. For instance, say something like OrderId. There is an advantage to defining it as an enum to get the type safety throughout C#. The actual enum values will not exist because they have a lifespan corresponding more with runtime vs compile time.
Similar:
9974127/serializing-object-with-invalid-enum-value
8913631/wcf-datacontract-class-with-enum-causes-enum-value-1-is-invalid-for-type-e