Question

Due to my error message:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Conversion failed when converting the nvarchar value 'AB' to data type int.

I have an enum with an AB value and I want to save it as integer not nvarchar value. I have an enum with flags attribute something like: [Flags]

public enum VisibleDayOfWeek : int
{
    None = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    Saturday = 32,
    Sunday = 64
}

I can not save multiple day string in the db but I can save the sum of the flags values which represent multiple days.

I do not want to create an integer wrapper around these enum properties.

The underlying type of an enum is a byte or integer so why the heck is it saved as string/varchar? That makes no sense. Even the entity framework got it right with its enum support after years...

What is the solution in this case?

Seems this guy has the same problem: https://github.com/tapmantwo/enumflags

Was it helpful?

Solution

Late answer, but in v4.0.8 the [Flags] attribute was added to enums, which makes ServiceStack always treat the emum as int.

From v4.0.54 you can also now use the [EnumAsInt] attribute which will save the enum to database as an int in OrmLite but when serializing it will do so as a string.

OTHER TIPS

As far as I know ormlite version 3 doesnt support enum, who know about next version. any way your solution there is just to use integer wrapper

public int VisibleDayOfWeek { get; set; }

[Ignored]
public VisibleDayOfWeek VisibleDayOfWeekEnum
{
    get { return (VisibleDayOfWeek)VisibleDayOfWeek; }
    set { VisibleDayOfWeek = (int)value; }
}

Even the entity framework got it right with its enum support after years

entity framework have enum support after version > 4, also you shouldn't expect to much entity framework is rich featured while ormlite is just just like its name 'lite'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top