Data modelling

Enums in Entity Framework

UI

Erp Core framework’s UI, automatically handles the rendering of enum collections. You can tell the UI to render the control as a search help or a list box by using either the searchHelp or listBox value for the UiDisplayAs attribute assigned to the enum list property. If you don’t assign any UiDisplayAs attribute, the UI will render by default a listBox.

List of enums rendered as a list box (default)List of enums rendered as a search help

The single enum value is rendered as a combo box by default. You can tell the UI to render the control as any of the single reference controls (searchHelp for example) by using the UiDisplayAs attribute assigned to the  property.

Data modelling

Enums are handled by Entity Framework as integer numbers. If you only need one value to be referenced by an entity, you just use it as a normal enum value in any class:

public ErpCalendarWeekDay ErpCalendarWeekDay { get; set; }

This will store the value as an int in the database and Entity Framework will automatically handle the value.

If you need a list of enum values to be stored in an entity, you need to store a list of integer values and map it to a list of enum values in your entity definition.

Here is an example (part of an entity) how you can store a list of enum values:

...
/// <summary>
/// The BYDAY rule part specifies a COMMA-separated list of days of
/// the week; SU indicates Sunday; MO indicates Monday; TU indicates
/// Tuesday; WE indicates Wednesday; TH indicates Thursday; FR
/// indicates Friday; and SA indicates Saturday.
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public List<int> ByDay { get; set; }

/// <summary>
/// Proxy for List<CalendarWeekDay>
/// </summary>
[NotMapped]
[UiFieldGroup(RULES, 130)]
public List<ErpCalendarWeekDay> ByDay_ 
{
	get => EnumExtensions
		.ToEnumMembersList<ErpCalendarWeekDay>(this.ByDay);
	set => this.ByDay = EnumExtensions
		.ToInt32List<ErpCalendarWeekDay>(value); 
}
...

In this example, the integer values are hidden from serialization and the casted values are serialized. At deserialization, the deserialized enum will set the integer values.

In the example above were used ErpCore methods present in the ErpExtensions package.