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 Query

More Query from same tag