Working with an application which uses a home-grown entity framework which comes as a referenced DLL (no source code), I have to access SQL data using its methods, like:
var allMyClasses = EF.ReadTable<MyClass>("select a, b, c from myTable")
This call returns IList<MyClass>. Whenever MyClass has a string member, which can take a value from a pre-defined list, I am tempted to refactor it into an enum as follows:
enum Things{ Something, Anotherthing }
class MyClass{
public int a {get; set;}
public Things b {get; set;}
public void set_b(string value)
{
if(Enum.TryParse(value, out Things th))
{
b = th;
}
else throw new ArgumentException("Unsupported thing: {}", th);
}
public string c {get; set;}
}
My assumption was that as long as there was an overloaded setter for b which took a string argument, conversion from the string retrieved from the DB to the enum Things should be automatic. Instead I keep getting a runtime exception:
System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'Things'.'
This is a shortcoming of the homegrown EF which does not know how to assign a string to an enum. Is there any way to work around that in my code without writing more than a few lines of code?