Entity Framework is an ORM (Object Relational Mapper) Framework that enables .NET developers to work with a database using .NET objects.
Before.NET 3.5, developers used to write Enterprise Data Access Block or ADO.NET code to retrieve or update application data from the database, where every time we used to open and close connection string.
Now with entity framework, you don’t need to worry about opening and closing connection, and it’s more easy to work with type safe data, because each database field is mapped with business object field.There are three ways we can work with Entity Framework
Let's start with an entity framework example of ado.net entity data model; here we learn how to implement entity framework in .net project step by step .
Step 1: right click on your .net project and add an "Ado.Net Entity Data Model" file as shown below. the file extension would be .edmx
Step 2: Now you can either create an empty model or create a model from database, in this example we create a model from existing database. , so select "Generate from Database".
Step 3: To connect database, you can create existing connection from list or create a new connection, here we create a new connection, so you can learn how to create a new connection, look at the picture below, and please use “SQL server authentication”, so the same user name and password can be used in connection string configuration.
Step 4: Once connected to database, you can choose the tables, views, store procedures you want to use in your project, or else select all, also specify the (relevant) model name.
Stp 5: Now your edmx file is loaded with database object information, so compile the project before you start writing code for database update and make sure you have set the connection string details properly.
Step 6 : Create a instance of DBContext class and start working with database, you can perform all CRUD operation using context object.
Note: if you are not sure about your DbContext object name, don’t worry, just open the edmx file, then right click and select properties, check the "entity container name" , that will be your DbContext name. Please look at the picture below.
Hope you enjoyed!
Now you can start practicing Add, Update, Select, Delete operation using entity framework