How to: Use Enumerations
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In this topic, you will learn how to work with Enum properties in the Visual Designer.
Suppose, you have an Order table like the one shown in the figure below. The OrderStatus column is of type int, it could accept null values and it will be mapped to an enum property in the Visual Designer.
Only columns of integer types could be mapped to Enum properties.
And you have created a domain model that looks like:
By default, the CLR type of the corresponding OrderStatus property will be Int32. To change the type of the OrderStatus column to an enum:
-
The first step is to create your enum. For example:
public enum OrderStatus { Unknown = 0, Processing, Shipped, Delivering, Cancelled }
Public Enum OrderStatus Unknown = 0 Processing Shipped Delivering Cancelled End Enum
-
Next, you need to change the type of the property in the Visual Designer.
Save your model.
How to Use Enum Properties Runtime
Working with enum properties runtime is easy. It is the same as using standard scalar properties. For example:
using ( EntitiesModel dbContext = new EntitiesModel() )
{
Order newOrder = new Order()
{
OrderDate = DateTime.Now,
OrderStatus = OrderStatus.Shipped
};
dbContext.Add(newOrder);
dbContext.SaveChanges();
List<Order> shippedOrders = ( from o in dbContext.Orders
where o.OrderStatus == OrderStatus.Shipped
select o ).ToList();
}
Using dbContext As New EntitiesModel()
Dim newOrder As New Order() _
With {.OrderDate = Date.Now, .OrderStatus = OrderStatus.Shipped}
dbContext.Add(newOrder)
dbContext.SaveChanges()
Dim shippedOrders As List(Of Order) = (
From o In dbContext.Orders
Where o.OrderStatus = OrderStatus.Shipped
Select o).ToList()
End Using