Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

From MySql to nullable Enum? with c# #182

Closed
mwlsilva opened this Issue · 7 comments

2 participants

@mwlsilva

Hello,

I Have a Enum Represented by:

public enum GenderType
{
Male = 0,
Female = 1
}

Then i have a MySql table ( User ) with a column called "Gender" with TinyInt Type ( also tryed with SmallInt having same problem )

On my code i have the Class user with a property like this:

    public GenderType? Gender { get; set; }

When i do:

DBaseFactory.DB().User.FindById(id);

I get the right dynamic object with Gender = 0 (SmallInt) or Gender=False (Tinyint)

when i cast it to Class: User where i have the property i typed above, i get my object.Gender=null

Does anyone know what am i doing wrong? The cast to Nullable enum seems to not work.

Thanks in advance for any help.

@markrendle
Owner

I appear to have some work to do on enum handling. It's worse than EF round here :)

@mwlsilva

Thanks for the answer Mark, would like to tell you that you are doing an awesome job with Simple.Data its by far the best way to communicate with the database.

Imho a lot better than what EF and other ORM´s offer today, with Simple.Data we get Clean SQL out of it and we have full controll so, congratulations, even without the nullable enum assignment it´s awesome ;-)

@markrendle
Owner

You are too kind :)

@markrendle
Owner

Hi. Can I ask what happens if you change the base type of the enum to byte (for tinyint) or short (for smallint)? E.g.:

public enum GenderType : byte
{
    Male = 0,
    Female = 1
}

That should make the casting work properly.

@mwlsilva

After changing the base type of GenderType to byte i noticed that the problem might be only at conversion from "object" to "persona":

public class Persona
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public GenderType? Gender { get; set; }
}

public Persona FindById(int id)
{
var p = DBaseFactory.DB().Persona.FindById(id);
if (p == null) { return null; }
//if (p.Gender != null) {
//p.Gender = (GenderType)Convert.ToInt32(p.Gender);
//}
return p;
}

If i inspect p at the return moment he has the correct value at Gender, the problem is that outside this "FindById" function the gender is null.

E.g:

( Persona per = FindById(1); )

At this point if i inspect "per" it will have Gender=null but when he was as a dynamic object inside FindById function he actually had the value =1 or 0.

My workaround is the code commented there, i explicitly check for the p.Gender and assign it a value converted. That way it works ok.

So Simple.Data seems to be doing his job correctly the problem must be converting from dynamic object to a class with a nullable enum on it, any idea how to get over this?

Thanks in advance

@markrendle
Owner
@mwlsilva

:-) Ok.

I´ll also try and search a bit more about it, if i find some hint or solution i´ll get it back to you.

Thanks

@markrendle markrendle closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.