Entity Framework
It is a tool to access the database. More accurately, it’s classified as an Object/Relational Mapper (ORM) which means it maps the data in a relational database into objects of our applications.

Before .NET Framework 3.5, developers used the ADO.NET Framework to open the connection, fetch the data from the database, insert the data to the database, create the database connection, make the DataSet containers, convert the data into the database, and then remember it to close the database connection as well. It was really a huge headache for the developers to write a large amount of code. Microsoft introduced Entity Framework (ORM) to make the code less cumbersome so you can do more. Now, we have predefined functions which insert our data, read our data and do everything you want to play with data.
Entity Framework provides the class DbContext which is actually the gateway or bridge which connects our model classes to the database. DbContext represents 1 or more DbSets which represents the table.

We use LINQ to query, these DbSets and EFs will translate our LINQ queries to SQL queries at runtime. It opens the connection to the database, reads the data, maps it to the object, and adds them to DbSet in the DbContext.

Why it is possible that we can apply LINQ to DbSet objects?
We can query our objects with LINQ. We know that DbSet properties are mapped to the Database in DbContext class but it is an object and we can apply LINQ to in-memory objects, database objects, and XML objects.
As we add/modify/remove the object in this DbSet, EF keeps track of these changes, and then when we ask it to persist the changes, again, it will automatically generate the SQL statements and executes them on the database.

Don’t be confused about the above description because when we’ll use Entity Framework, these things will automatically be clear to you.
There are two ways or two workflows to use EF.
- DB First
- Code First
We’ll cover both the approaches in our ASP.NET MVC 5 Series.
Database First
This is the traditional approach in most of the software houses because most of the things they are doing are database dependent. Let me explain.
Actually, the database isn’t just all about storing the data, creating tables, and filtering the results. The topic the of the database is so vast these days, we can define stored procedures, functions, views, and we can define the transactions to make the most of the application logic at database side. The benefit of this approach is we make most of the application logic at the database side, so we need to just implement it in the application side. But if we write these logics in the application, then surely we need to write hundreds of lines of code.

Code First
In Code First approach, we make the model classes in our application first. And here we can apply the validations on the properties as well. Entity Framework automatically maps our model classes and creates the tables for us according to our model class. The properties of model classes define the fields of the table. If we try to change our model classes to change the database, then we need to handle these changes through Migrations.
If you see most of the tutorials, then you’ll get to the point that most of the instructors are going with this approach because we can do anything with this approach.
But honestly speaking, don’t just learn code first -- you should also know how to implement a database first approach for job perspective, because the database first approach makes your database development and designing better as well.

But if you see from another perspective, most of the code we’ve already done here our model classes are ready, so this approach makes it easy for us to implement the testing.
First of all, we’ll implement Code First Approach to learn some programming concepts, entity framework and how it works under the hood, how we can query the database objects and then we’ll move to the database first approach.


Ahsan SiddiquePosted May 28, 2019, 11:47 AM
Thank you boy for explaining in simple words.
ShehzadPosted May 23, 2018, 6:20 PM
Hi...is linq prerequisite to understand /work on entity frame work code first approach?....to clarify i understand concept of delegates...anonymous method...lambda expression.