Types of entities

Entity Framework
5.0
TEAM
RITCHIE NITHOO, AMER NASSER EL DINE,
VINCENT MARCHAL, MICHEL KNOERTZER
MANAGER
REDA BENDRAOU
Entity Framework
ORM : Object Relationnal Mapping
 Avoid database knowledge
 Oriented object queries
 Scalable, stable
 Easy to use
Databases supported
Databases
 SQL Server
 Oracle
 DB2
 MySQL
 And others…
Easy to switch between them
Architecture
Oriented object queries
Create entities
Translate queries in SQL
Ask the database
Queries examples
LINQ to Entities
Entity SQL
Native SQL
Types of entities (1)
Entity Objects
 (+) Track changes, support lazy loading, efficient
 (-) Strong dependency on the EF
If require persistence ignorance
 POCO or POCO proxy
Types of entities (2)
POCO (Plain Old CLR Object)
 (+) Support LINQ queries like Entity Object
 (+) Persistent ignorant
 (-) Save changes manually : call the Object Context
Types of entities (3)
POCO Proxy
 (+) Enable the lazy loading proxy
 (+) Instant & automatic change tracking : More efficient than POCO
 (-) Need some requirements
Types of entities (4)
Self-Tracking Entities
 (+) Efficient in n-tier application
 (-) Must implements IObjectWithChangeTracker & INotifyPropertyChanged
 (-) A lot of code
[DataMember]
public int StudentID {
get { return _studentID; }
set {
if (_studentID != value) {
if (ChangeTracker.ChangeTrackingEnabled && ChangeTracker.State != ObjectState.Added) {
throw new InvalidOperationException("The property 'StudentID' is part of the object's key and cannot be changed.
Changes to key properties can only be made when the object is not being tracked or is in the Added state.");
}
_studentID = value; OnPropertyChanged("StudentID");
}
}
Modeling techniques
Code first
 Generate database from code
 Avoid working with the model designer
Model first
 Generate database and code from a model
Database first
 Generate a model from database
Entities Lifecycle
Entities relationships
Same relationships like hibernate
 One to One
 One to Many
 Many to Many
 Many to Many relationships are managed automatically (join
tables)
Projection Query
Process of selecting data in a different shape than
the queried entity
 Knowledge of LINQ
 FirstorDefault<>
 ToList<>
Eager Loading
Eager loading is the process whereby a query for
one type of entity also loads related entities as
part of the query.
Lazy Loading
Lazy loading means delaying the loading of
related data until your specifically requested.
Explicit Loading (1/2)
Possibility of loading related entities explicity using
the method Load().
Explicit Loading (2/2)
Possibility of applying filters in the explicit loading
Persistence in Entity Framework
 Persistence refers to the characteristic of state that outlives the
process that created it.
 Persistence consist to store the state as data in non-volatile storage
 How Entity framework manage the persistence ?
 Connected Scenario :
The entity is retrieved from the database and modified in the same
context
 Disconnected Scenario : The entity is modified in a different
context
SaveChanges Method (1/2)
 Persist all changes made to entities to the database.
 Perform insert, update or delete operation.





Open a connection
Start database connection
Pushes all pending changes
Commit transaction
Dispose the connection
SaveChanges Method (2/2)

Exemple :
//Update entity using SaveChanges method
using (SchoolEntities ctx = new SchoolDBEntities())
{ var stud = (from s in ctx.Students
where s.StudentName == "Student1" select s).FirstOrDefault();
stud.StudentName = "Student2" ;
int num = ctx.SaveChanges(); }
Save new Entity with ObjectContext

Exemple :
Student student = new Student();
student.StudentName = "Student1" ;
using (var ctx = new SchoolDBContext())
{
ctx.Students.AddObject(student);
ctx.SaveChanges();
}
Update Entity : Connected mode

Exemple :
using (var ctx = new SchoolDBContext())
{
var stud = (from s in ctx.Students
where s.StudentName == "Student1"
select s).FirstOrDefault();
stud.StudentName = "Updated Student1" ;
}

int num = ctx.SaveChanges();
ObjectStateManager of the context keeps tracks of current and
original values of the student entity.
Update Entity : Disconnected mode

Exemple :
Student stud = null ;
using (SchoolDBContext ctx = new SchoolDBContext())
{
ctx.ContextOptions.LazyLoadingEnabled = false;
stud = (from s in ctx.Students where s.StudentName == " student1"
select s).FirstOrDefault();
}
//Out of using scope so ctx has disposed here
stud.StudentName = "Updated student1" ;
using (SchoolDBContext newCtx = new SchoolDBContext())
{
newCtx.Students.Attach(stud);
newCtx.ObjectStateManager.ChangeObjectState(stud,
System.Data.EntityState.Modified);
newCtx.SaveChanges();
}
Delete Entity : Connected mode

Exemple :
using (var ctx = new SchoolDBContext())
{
var stud = (from s in ctx.Students
where s.StudentName == "Student1"
select s).FirstOrDefault();
ctx.Students.DeleteObject(stud);
}
int num = ctx.SaveChanges();

Remove student entity from Students entityset.

SaveChanges delete student row from student table in the DB.
Delete Entity : Disconnected mode

Exemple :
Student stud = null;
using (SchoolDBContext ctx = new SchoolDBContext())
{
ctx.ContextOptions.LazyLoadingEnabled = false;
stud = (from s in ctx.Students where s.StudentName == " student1"
select s).FirstOrDefault();
}
using (SchoolDBContext newCtx = new SchoolDBContext())
{
newCtx.Students.Attach(stud);
newCtx.Students.DeleteObject(stud);
}
newCtx.SaveChanges();