Link Search Menu Expand Document

Enumeration


This feature enables working with enumeration objects within class properties. The library supports various transformation modes for enumerations.

Property String

Given a table named [dbo].[Person] with the following structure:

CREATE TABLE [dbo].[Person]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](128) NOT NULL,
    [Gender] [nvarchar](16) NOT NULL,
    [CreatedDateUtc] [datetime2](5) NOT NULL,
    CONSTRAINT [CRIX_Person_Id] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
)
ON [PRIMARY];
GO

Define the enumeration:

public enum Gender
{
    Unknown,
    Male,
    Female
}

Map it to the Gender column of the Person class:

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Gender Gender { get; set; } // Enumeration (As String)
    public DateTime CreatedDateUtc { get; set; }
}

Enumeration values are saved in the database as string.

Property String (As Int)

To force the value to be saved as int, use the TypeMap attribute.

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    [TypeMap(DbType.Int32)]
    public Gender Gender { get; set; } // Enumeration (As Forced Int)
    public DateTime CreatedDateUtc { get; set; }
}

Or via FluentMapper:

FluentMapper
    .Type<Gender>()
    .DbType(DbType.Int32);

Or via TypeMapper:

TypeMapper
    .Map(typeof(Gender), DbType.Int32);

Property Int

Given a table with an integer Gender column:

CREATE TABLE [dbo].[Person]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](128) NOT NULL,
    [Gender] [int] NOT NULL,
    [CreatedDateUtc] [datetime2](5) NOT NULL,
    CONSTRAINT [CRIX_Person_Id] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
)
ON [PRIMARY];
GO

Define the enumeration:

public enum Gender
{
    Unknown,
    Male,
    Female
}

Map it to the Gender column of the Person class:

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Gender Gender { get; set; } // Enumeration (As Int)
    public DateTime CreatedDateUtc { get; set; }
}

Enumeration values are saved in the database as int.

Default Conversion

By default, the library uses DbType.String for all enumeration conversions in non-model-based operations (e.g., ExecuteScalar, ExecuteNonQuery, and ExecuteReader). Override this behavior by setting EnumDefaultDatabaseType to any DbType.

GlobalConfiguration
    .Setup(new()
    {
        EnumDefaultDatabaseType = DbType.Int32
    });

For model-based operations (e.g., Query, Update, Merge), the default conversion is not applied — the correct database type is inferred from the underlying table schema.

PropertyHandler

A property handler can be created to manually control enumeration transformation by implementing IPropertyHandler.

public class PersonGenderPropertyHandler : IPropertyHandler<string, Gender?>
{
    public Gender? Get(string input, PropertyHandlerGetOptons options)
    {
        if (!string.IsNullOrEmpty(input))
        {
            return (Gender)Enum.Parse(typeof(Gender), input);
        }
        return null;
    }

    public string Get(Gender? input, PropertyHandlerSetOptons options)
    {
        return input?.ToString();
    }
}

Map it via the PropertyHandler attribute:

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    [PropertyHandler(typeof(PersonGenderPropertyHandler))]
    public Gender Gender { get; set; }
    public DateTime CreatedDateUtc { get; set; }
}

Enumeration auto-mapping is bypassed when a property handler is mapped to an enumeration property. Using a property handler gives you full control over the transformation.

Query Expression

Enumerations can be used directly in query expressions.

using (var connection = new SqlConnection(connectionString))
{
    var females = connection.Query<Person>(e => e.Gender == Gender.Female);
}

Or in raw SQL:

using (var connection = new SqlConnection(connectionString))
{
    var param = new
    {
        Gender = Gender.Female
    };
    var females = connection.ExecuteQuery<Person>("SELECT * FROM [dbo].[Person] WHERE [Gender] = @Gender;", param);
}

Type Inference

Type inference is supported, allowing you to infer an enumeration directly when fetching rows via ExecuteQuery.

using (var connection = new SqlConnection(connectionString))
{
    var genders = connection.ExecuteQuery<Gender>("SELECT [Gender] FROM [dbo].[Person];");
}