Data Access has been discontinued. Please refer to this page for more information.

Enum Support

Telerik Data Access Fluent Mapping API provides built-in support for enum properties. Suppose, you have a class with an enum property like the one shown below:

public class Order
{
   public int Id{get;set;}
   public OrderStatus OrderStatus{get;set;}
}

public enum OrderStatus
{
   Unknown = 0,
   Processing,
   Shipped,
   Delivering,
   Cancelled
}
Public Class Order
    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _orderStatus As OrderStatus
    Public Property OrderStatus() As OrderStatus
        Get
            Return _orderStatus
        End Get
        Set(ByVal value As OrderStatus)
            _orderStatus = value
        End Set
    End Property
End Class

Public Enum OrderStatus
    Unknown = 0
    Processing
    Shipped
    Delivering
    Cancelled
End Enum

You can map your enum property just like any other scalar property. The corresponding database column will be of type int. For example:

MappingConfiguration<Order> configuration = new MappingConfiguration<Order>();
configuration.MapType( x => new
{
   ID = x.Id,
   OrderStatus = x.OrderStatus
} ).ToTable( "Orders" );
Dim configuration As New MappingConfiguration(Of Order)()
configuration.MapType(Function(x) New With {
       Key .ID = x.Id, Key .OrderStatus = x.OrderStatus}).ToTable("Orders")
configuration.FieldNamingRules.AddPrefix = "_"

Only columns of integer types could be mapped to enum properties.

Using Enum Properties Runtime

Working with enum properties runtime is easy. It is the same as using standard scalar properties. For example:

using ( FluentModelContext dbContext = new FluentModelContext() )
{
   Order order = new Order
                       {
                           OrderStatus = OrderStatus.Shipped,
                       };
   dbContext.Add( order );
   dbContext.SaveChanges();
}
Using dbContext As New FluentModelContext()
 Dim _order As Order = New Order With {.OrderStatus = OrderStatus.Shipped}
 dbContext.Add(_order)
 dbContext.SaveChanges()
End Using