Microsoft Entity Framework
Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework
with LINQ, it allows to retrieve and manipulate data as strongly typed objects. This ORM implementation provides services like
- change tracking
- identity resolution
- lazy loading
- and query translation
EDM (Entity Data Model): contains
- Conceptual model -- classes
- Mapping - mapping of relations of models
- Storage model. - actual db schema
Some other terms
ERM (Entity Relationship Model)
UML (Unified Modeling Language)
ORM (Object-Relational Mapping)
DFD (Data Flow Diagram)
four types of Entities
1) EntityObject
public partial class Person : EntityObject{...} // generated code
2) POCO
public class Person{...} // manual code
3) POCO Proxy
POCO Proxy is a runtime proxy class of POCO entity.
difference between POCO and POCO Proxy lies in save changes mechanism. POCO entities use Snapshot mechanism. So before saving changes POCO entities, you must use DetechChanges method of ObjectContext to synchronize data with ObjectStateManager.
4) Self-Tracking Entities.
Self-Tracking entities have additional change tracking functions and it implements IObjectWithChangeTracker and INotifyPropertyChanged interface. It also mark it as DataContract to be used in WCF services.
[DataContract(IsReference = true)]
[KnownType(typeof(Standard))]
[KnownType(typeof(StudentAddress))]
[KnownType(typeof(Course))]
public partial class Person: IObjectWithChangeTracker, INotifyPropertyChanged
{ ...}
Modelling approaches
Code First - (starts with POCO)
approch with DbContext -- alternative to ObjectContext
approach with DbSet. alternative to ObjectSet -- for CRUD operations
Model First
Use VS designer to create DB - It gives a SQL to create DB schema too
Database first
Reverse engineer entities from database (on existing DB)v
Context delegates change-tracking to ObjectStateManager. ObjectStateManager tracks changes in the context. Until save is called.
Using(PeopleDBEntities ctx = new PeopleDBEntities())
{
var objStateMgr = ctx.ObjectStateManager;
int num = ctx.SaveChanges();
}
Lazy loading
delaying the loading of related data(relational data of other model) until you specifically request it
EagerLoading
explicitly avoid lazy loading by .Include("RelatedTableData")