0
Your Cart
No products in the cart.

Entity Framework Tutorials using C#

It will add the reference to the Entity Framework in the reference folder. Let’s understand what entity framework can do for us with an example. Assume we have the following 2 tables (Departments and Employees). Because of the extracted transaction handling it’s really convenient to group operations together and commit or roll them back as a single unit. This greatly improves data consistency, since nothing will be saved in case of an exception, but it is guaranteed that everything will be saved if there is no error.

As already discussed, we need to use the Entity Framework Code First Approach when we do not have an existing database for our application. The following image shows the working style of Entity Framework Code First Approach. As you can see in the below image, the Entity Framework creates the database based on the domain classes and the context class. That means, in this approach, we need to start coding first in C#.NET or VB.NET and then Entity Framework API will create the database from your code. Entity Framework is an open-source object-relational mapper framework for .NET applications supported by Microsoft. It eliminates the need for most of the data-access code which is used to interact with the database that developers usually need to write.

The Unit of Work Implementation

So, create a class file with the name EFCodeFirstContext.cs and then copy and paste the following code into it. But the class should and must be derived from DbContext class and exposes DbSet https://deveducation.com/ properties for the types that you want to be part of the model. For example, Student and Standard domain classes in this case. The DbSet is a collection of entity classes (i.e. entity set).

what is entity framework in asp net

The ORM Framework sits between our application code and the Database. It eliminates the need for most custom data-access codes we usually write without an ORM. In this article, we did a deep dive into the unit of work pattern.

Create migration

We have achieved all this without writing a single line of ADO.NET code. If this is not clear at this moment how it works, don’t worry we will discuss everything in detail in our upcoming articles. Please use the below SQL script to create the database EF_Demo_DB, and tables Departments and Employees, and populate the tables with sample data. We want to display the data from both tables (Departments and Employees) in a console application as shown below. Another issue related to delays occurs when we retrieve an entity from the database, make modifications within the unit of work, and then re-query it within the same transaction. In this scenario, it retains the previous values because we haven’t saved the changes yet.

As you can see in the above code, we start with the Entity() method. Most of the time, we have to start with the Entity() method to configure the Entity using Fluent API, and here, we need to provide the Entity name for which we want to provide configuration. Then, we what is entity framework used the ToTable() method to map the Student entity to the StudentInfo table and the Standard entity to the StandardInfo table. Here, the StandardInfo table will be created with the Admin schema because we have specified the Admin schema for the StandardInfo table.

In this Entity Framework Tutorials Course using C#, I am going to discuss all the concepts of Entity Framework with Real-Time Examples. Here in this Entity Framework Tutorials article series, we will start from the very basics, and as we progress, we will cover all the intermediate and advanced features of Entity Framework as well. Now copy and paste the following code in the main method of the program class as shown below.

what is entity framework in asp net

Add a Comment

Your email address will not be published.

All Categories

Quick Call

Talk to an expert