Skip to main content

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6 by Ziggy Rafiq

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6


An Enum (Enumeration) is a group of constants that are read-only value types. By default, the first value of the Enum variable is 0 i.e. Here we will create an Enum of Priorities type with read-only values of Highest, Normal and Low. We will set the read-only values using an integer assigning a number next to the value. By default, the integer value will start with 0. Here we will be assigning the integer value next to the Enum value such as in the below example and we will use a comma (,) to separate the item in the list of Enum(Enumeration).  We create Enum by using the Enum keyword and then using class, interface, and abstract.

The reason we use an Enum is to ensure we improve our application performance and improve application readability, and maintainability, and reduces the complexity of the application hence why if you take a look at the example below of Status (NotStarted, Started, Completed, OnHold) the first value is the default value, which is 0, whereas on the above example of Priority Enum I have defined the integers values and the example below of Status Enum we have not to define integers values next to the value type as Enum will set the values itself starting with 0.

The catch here is that it is up to us as a developer to ensure we define the types of the values or not depending on the need of using the Enum. One thing I will say is that if we were to create Enum for let’s say Publish and the values are No and Yes then we will not need to really set an integer value next to the Enum data value.

Demo of using Enum with EF Core Model 

Here we will be creating a very basic and simple Data Model using Entity Framework Core with Microsoft SQL Server Database and we will be populating data by using the Model Builder extension method.  In this section, you will be able to gain an understanding of Entity Framework Core and how we use its extensions to build our database. You can download these blog post source code from my GitHub Responsibility

The Entity Framework Core Project is a Class Library and has a Model of Task and Enums of Priority and Status. In this demo, you will also be able to gain an understanding of Ambiguous reference types. As you will in the Model folder we have a class name Task, which is also an ambiguous reference to System.Threading.Tasks.Task and to overcome this we have a few different ways to do it, which are if the type is in a small project like this one we created an alias for Task type like this:

using MyTask = EnumWithEfCoreDemo.Models.Task;

or if there is more than one type of ambiguous i.e. we can more than these simple Task types then we can use an alias like this 

using Task1 = EnumWithEfCoreDemo.Models.Task;
using Task2 = SomeOther.Models.Task;
using Task3 = SomeOtherOneMore.Models.Task;

and we use the type like this in our code Task1 

Step 1

Download the Demo Project from my GitHub Repo, which is on the following URL Address

Step 2

Unzip the Demo Project and double click on the  EnumWithEfCoreDemo.sln 

Step 3

Once the project is opened in your IDE my is Visual Studio 2022 Community Edition. We will need to modify the appsettings.json file with our database connection string, where it says DefaultConnection : Data Source =[Add Your Ms SQL Server Name]; Please overwrite the [Add Your Ms SQL Server Name] with your SQL Server name.

Overwrite the [Add Your Database Name], where it says Initial Catlog=[Add Your Database Name]  You can give it whatever Database name you like or you can name it EnumWithEfCoreDemoDb it is all up to you.

And then save the appsettings.json file and open the Package Manager Console if you are using Visual Studio 2019/2022 and type in Add-Migration EnumWithEfCoreDemoDbBuild and press enter to generate the Ef Core Migration Script.

Now type in the Package Manager Console Update-Database and it will create the database and populate it.

 If you want to use the .Net Core CLI then you can open up the command window and type dotnet ef core migration add EnumWithEfCoreDemoDbBuild and generate the ef core migration script and then type EF core database update to execute the script and create the database and populate it.

Final Thoughts 

We have in this blog post gained an understanding of how to use Enum with Entity Framework Core and also, we have gained a basic understanding of Enum data values and we had a very quick look at the Ambiguous Reference Types and how to overcome this issue/concern.

In my next blog post, we will be creating a simple REST API using Core and we will be using this simple demo Project as our Database I will be showing you how we can create a REST API and carry out simple Create, Read, Update and Delete (CRUD) operations using Core REST API with Entity Framework Core using the Best Coding Practices and Keeping a Clean Architecture.


Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b&

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, lists,