Friday, 4 October 2024

Entity Framework

 Entity Framework is an Object relation Management (ORM) from Microsoft 

ADO.NET Vs Entity Framework

ADO.NET

Entity Framework

Ado.net is faster

EF will be around the ADO.net which means ADO.NET is faster then EF

We need to write so much code to talk to database

Easy to use.

As an EF will talk to database without much code involved

Performance is better than EF

Performance is not good compared to ADO.NET


Linq to SQL Vs Entity Framework

LINQ to SQL

Entity Framework

Supports only for SQL database

Supports database like SQL,Mysql,DB2 etc.

Allows only one to one mapping between the entity classes and the relational table

Allows one to one , one to many & many to many mappings between the entity classes and the relational tables

.dbml file is generated for maintaining the relationship

Relationships are maintained in 3 different files .csdl,.msl and .ssdl


Different Approaches in EF

  1. Database First 
  2. Model First
  3. Code First
Database First:
We have a database already created and ready to use it.
Using the existing database , we can create the entity Models.

Model First:
Create the Entity Models First and derive the database from the Entity Models.

Code First:
Create the Domain classes first and derive the database from the domain classes.

Three Ways of load entities in Entity Framework

  1. Lazy Loading
  2. Eager Loading
  3. Explicit Loading

Lazy Loading: 
  •     Dependent/related entities are loaded once they are accessed for the First time
  •     Delay the loading of related object until it is required
e.g Student s = dbcontext.students.FirstOrDefault(a => a.StudentId == SId)



The Entity Framework requires your navigation properties to be marked as virtual so that lazy loading and efficient change tracking are supported

Eager Loading: Will do only one database call and get all the dependent entities
e.g Student s= dbcontext.students.Include(s=>s.Courses).FirstOrDefault(s=>s.studentid == sid).

Explicit Loading: - By default it will be lazy loading , But we can disable the lazy loading and we can still load the dependent/related entities by calling Load method.

Student s = dbcontext.Students.FirstOrDefault(a=>a.studentID == sid);
dbcontext.Entry(s).Reference(s=>s.courses).Load();

Connected Vs disconnected data access in ADO.NET

Connected - DataReader Object 
Dis-Connected - DataAdapter Object 













No comments:

Post a Comment