What is Entity Framework? A start point for beginners.

Reading Time: 5 minutes

Introduction

Entity Framework is an Object Relational Mapper (ORM) simplifies mapping between objects in your software to the tables and columns of a relational database.

In this article I wanna go over the basics of entity framework so you follow up with me in my courses, it is not a detailed explanatory article, it’s more an example to start with and learn the idea of entity framework.

To make things clear. usually, when you have a data-driven application, let’s say with SQL server backend for example. You create your tables, relations, stored procedures, and all database stuff. Besides that, you create your application interface whether it’s a desktop or web application.

Then you create a connection between your application and the database programmatically or using datasets.

In this scenario, the database is somehow completely isolated from the application.

You open a connection, execute your queries then close the connection.

Entity Framework

Now with Entity framework all, your database objects(tables, procedures, relation) will be mapped as classes in your application.

Let’s see how in this simple example.

1. Create a new windows forms c# application project in visual studio.

2. Create a sample database with 2 tables, one stored procedure, and a relation in SQL server.

Entity Framework

2. In visual studio, Right-click on the project in the solution explorer and click Add > New item, and search for “entity data modal”

Entity Framework

3. Click Add and a wizard will be shown

Note: If you didn’t find the “ADO.NEt data model template” then you have to install entity framework from NuGet package manager as shown below:

Entity Framework h-educate

4. In the wizard click “EF Designer from database” and click next.

5. Now select your connection and test it if everything is fine. then click next.

Entity Framework h-educate

6.Click Next and select tables and procedures as shown below:

Entity Framework h-educate

7. Click finish and wait for the classes to be generated. when the process finishes you will see a new edmx file created, open it (it may open automatically).

Entity Framework h-educate

You can navigate through the model browser to see all the mapped objects.

Entity Framework h-educate

To see the generated classes for our database, go back to solution explorer > select the generated model and expand the node, then you can find the classes as shown in the figure:

Entity Framework h-educate

Insert, update and delete.

 

Insert new record example:


testEntities db = new testEntities(); //create a new instance of your entities
NameTbl NewName = new NameTbl(); //create a new instance of the table you want insert in
NewName.Name = "hassan"; // set the values as properties in your code
NewName.email = "hassan.cs91@gmail.com"; // set the values as properties in your code
db.NameTbls.Add(NewName); call the add function to add the new object
db.SaveChanges(); //save the database

Delete record example:


testEntities db = new testEntities(); // new instance of the enitity
NameTbl NewName = db.NameTbls.Find(1); // get the record object you want to delete
db.NameTbls.Remove(NewName); // remove the object
db.SaveChanges(); //save the database

Update record example:


testEntities db = new testEntities(); // new instance of the enitity
NameTbl nametoEdit = db.NameTbls.Find(1); // get the record object you want to edit
nametoEdit.email = "newemailvalue"; //edit the property value
db.SaveChanges(); //save the database

Execute a stored procedure.


testEntities db = new testEntities(); // new instance of the enitity
db.GetNames(); //execute the stored procedure directly

 

I hope this gave you an overview of how to work with entity framework.

Please if you have any question, feel free to leave it in the questions area.

 

For more details and detailed explanation check out these links:

 

Leave a Comment

Your email address will not be published. Required fields are marked *