0

I am trying to map tinyint column to byte property in c#How to achieve this efficiently. I want to store the values null, 0 and 1 to the database column. Please find below my code. Can anyone help me whether the following is the right approach?

public enum TriState : byte
{
    Null = 0,
    True = 1,
    False =2
}

[NotMapped]
public TriState AuthorisationStatus { get; set; }

[Column("AuthorisationStatus")]
public byte AuthorisationStatusByte {
    get
    {
        return Convert.ToByte(AuthorisationStatus.ToString());
    }
    private set
    {
        AuthorisationStatus = EnumExtensions.ParseEnum<TriState>(value);
    }
}

public static T ParseEnum<T>(byte value)
{
    return (T)Enum.Parse(typeof(T), value.ToString(), true);
}

Thanks

1 Answer 1

2

There's no need to go via a string at all. Just use the explicit conversions:

[Column("AuthorisationStatus")]
public byte AuthorisationStatusByte
{
    get { return (byte) AuthorisationStatus; }
    set { AuthorisationStatus = (TriState) value; }
}

(I'm assuming the column attribute is correct - I don't know about that side of things.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. How about if it is byte? i.e nullable byte
@MukilDeepthi: Then you'd presumably want a nullable enum property as well... and you'd just add a ? within each of the casts as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.