Database First Approach in Entity Framework Step by Step

Introduction

Entity Framework is an Object Relational Mapper. What is meant of Object Relational Mapper (ORM)?

It's means in simple words:

  1. If you want to use RDBMS tables as classes, the table's attributes as properties and its procedures as methods in C# applications then it will map all the relational database objects into C# objects like properties and methods.

  2. If you want to create a table from C# code in SQL Server and work on it then it generates the required SQL scripts for creating database objects.

SO ORM works to map C# code in a SQL Server sript for creating the database and table and vice versa the RDBMS table to C# objects.

Step 1

Create a database and tables.

  1. CREATE DATABASE ENTITYDB  
  2. GO  
  3. USE ENTITYDB  
  4. GO  
  5. CREATE TABLE tblStudent  
  6. (  
  7. ID INT PRIMARY KEY IDENTITY(1,1),  
  8. NAME VARCHAR(50),  
  9. GENDER VARCHAR(10),  
  10. AGE INT,  
  11. COURSE VARCHAR(50)  
  12. )  
  13. GO  
  14. INSERT INTO tblStudent VALUES('MACK','MALE',20,'BCA'),('JACK','MALE',24,'MCA'),('RIYA','FEMALE',21,'BCA'),('TOM','MALE',23,'MCS')  

Step 2

Now add a project and choose console application.

console application

Step 3

Now go to Add New item -> ADO.NET Entity Data Model and provide an appropriate name.

Entity Data Model

Step 4

generate from database


Step 5

Now fill in the required information as in the following:

connection

Step 6

Now choose new connection and add your databse. Here there are the following two checkboxes.

  1. The first checkbox tells you "Do you want to exclude sensitive data like your password from connectionstring if you check it then it exclude your sensitive data from configuration file".

  2. The second checkbox tells you "Do you want to include sensitive data like your password in connectionstring if you check it then it incclude your sensitive data in configuration file".

    data connection

Step 7

Now go and choose your table. I selected tblStudent from the table list.

table

Step 8

After finishing a model has been created like this.

model

Basically when you finish the entity wizard then add a System.Data.Entity DLL is in your references. It's the base for working with Entity Framework.

And one Context class has been added to your edmx file. If you follow me then you go to "StudentModel.edmx" -> "StudentModel.Designer.cs" and look for "ENTITYDBEntities".

"ENTITYDBEntities" is the context class for this edmx file.

ENTITYDBEntities

Step 9

Now write the following code in the console application.

  1. using System;  
  2. using System.Linq;  
  3.   
  4. namespace LearnEFStepbyStep  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             using (ENTITYDBEntities cx = new ENTITYDBEntities())  
  11.             {  
  12.               var students=cx.tblStudents.ToList();  
  13.               foreach (var student in students)  
  14.               {  
  15.                   Console.WriteLine("StudentID : "+student.ID+"  Student Name : "+student.NAME+" Gender : "+student.GENDER+" Age : "+student.AGE+" Course : "+student.COURSE);  
  16.               }  
  17.               Console.ReadKey();  
  18.               
  19.             }  
  20.   
  21.         }  
  22.     }  
  23. }  
Step 10

Now run it.

output

Conclusion

Here, I am trying to simplify all the terms related to Entity Framework in a very easy way.

Up Next
    Ebook Download
    View all
    Learn
    View all