score:2

Accepted answer

The exception is because of you saved your mysql enum value as string true, false and this is not parsed by enum in c# so that error comes.

So if there is possible then change the enum in MySQL like

`reconciled` enum(1,0) NOT NULL DEFAULT 0

And then set your c# enum member value to 0,1 like

public enum BankAccountReconciled
{
    [Display(Name = "true")]
    True = 1,                    //<= Set 1 for true that equivalent to mysql enum value 1
    [Display(Name = "false")]
    False = 0                    //<= Set 0 for false that equivalent to mysql enum value 0
}

Edit:

One way is that just keep you mysql enum as it is like

`reconciled` enum('true','false') NOT NULL DEFAULT 'false',

And keep your class property datatype to string

public string Reconciled { get; set; }

And keep your c# enum with respect to mysql enum

public enum BankAccountReconciled
{
    [Display(Name = "true")]
    True,                      
    [Display(Name = "false")]
    False                       
}

And now just add one more property to your class object that can cast your string to enum like

public BankAccountReconciled EnumReconciled
{
    get
    {
        return GetEnumValue<BankAccountReconciled>(Reconciled);
    }
}

And above property need a helper function that can convert your string to enum

public static T GetEnumValue<T>(string str) where T : struct, IConvertible
{
    Type enumType = typeof(T);
    if (!enumType.IsEnum)
    {
        throw new Exception("T must be an Enumeration type.");
    }
    T val;
    return Enum.TryParse<T>(str, true, out val) ? val : default(T);
}

Output: (from Debugger)

enter image description here

score:0

The orders column is marked as null on the database, but not as a nullable type in .NET (property Orders).


Related Articles