Entity Framework is a Platform independent, open-source ORM (object-relational mapping) framework provided by Microsoft to automate Database related activities in applications. It fits between business logic and database, in other words, it provides decoupling between the two. It uses LINQ queries to access the database.
To use the entity framework in a project one needs to add an entity framework package in the project. Entity Framework uses three approaches to use the database. The approaches are
- Model first.
Database-First approach:– In this approach, we make a database first by using a visual designer and then the entity framework generates the model classes according to the table generated on the database.
In this we first go to the SQL server management studio we either select the existing database or create a new database and then generate a table under it. Then we go to the project and import this database into the project. The code will be auto-generated by the entity framework in a .cs file and the class will have an extension DBContext which is an abstraction of database which is a simple API to load data from the database. We also use database properties to use database tables.
Code-First approach:– We start with the code by simply creating the domain classes first and the entity framework generates the database tables according to this. This is the new approach applied by the entity framework.
In this approach, we create a class in the .cs file with the no of elements we want to use in a table and then we create another context class with an extension of DBContext and we name the table under which we want to use the properties. In this, we also need to give a connection string in the app.config file. Then the entity framework will provide the table and its columns in the database automatically.
Model-first:– In this approach we use visual designer to model the tables and there associations in some kind of UML diagram. Then the entity framework model the classes and database according to the design.
In this a visual representation of table is generated in visual designer with the no of elements and the entity framework will generate the code and table according to that approach. This approach is rarely used by developer because of the poor accessibility and user interface.