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 https://github.com/ziggyrafiq/.Net-6-EF-Core-with-Enum-
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
https://github.com/ziggyrafiq/.Net-6-EF-Core-with-Enum-
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 ASP.net 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 ASP.net Core
REST API with Entity Framework Core using the Best Coding Practices and Keeping
a Clean Architecture.
Comments