Entity Framework

  • EF is an Open Source ORM Framework
  • EF allows for a more granular level mapping between Entities and Database Tables. Conceptual Model

    For e.g., by mapping the single entity to multiple database tables or even multiple entities to a single table.

  • EF Works with any relational database with valid Entity Framework provider.
  • Tracks changes to in-memory objects
  • Has stored Procedure Support


alt text

Object Services

ObjectContext is the core of the Object Services Layer

  • Support for state management, optimistic concurrency and identity resolution
  • Support for lazy loading, inheritance and navigation relationships
  • Support for query transformation, materialization and change tracking

Entity Client Data Provider

The main responsibility of this layer is to convert common SQL languages(like Linq to Entities or Entity SQL) queries into a SQL query

Entity Data Model (EDM)

  • Conceptual Model
    • Contains the model classes and their relationships
    • This is independent from your database table design
  • Mapping
    • Information about how the conceptual model is mapped to the storage model
  • Storage Model
    • Storage model is the database design model which includes tables, views, stored procedures, and their relationships and keys.

ADO.Net Data Provider

  • Source Specific Providers to abstract the ADO.NET interfaces.
  • Translates LINQ to native SQL expression via command tree
  • Executes it against the specific DBMS system.

Entity Data Model

Schema Definition Language (SDM)

ADO.NET Entity Framework uses an XML based Data Definition Language called Schema Definition Language (SDL) to define the EDM Schema.

Key Concepts

  • Entity type
  • Association type
  • Property

Entity Type

  • An entity represents a specific object
  • Each entity must have a unique entity key within an entity set
  • An Entity Set is a collection of instances of a specific entity type (Object representation of Table)

Association type

  • Describes the relationship in Entity Data Modal
  • Every association has two association ends that specify the entity types involved in the association.
  • Each association end also specifies an Association End Multiplicity that indicates the number of entities that can be at that end of the association.

    • Association End Multiplicity
    • Allowed Values: 1, 0 or 1, *
    • An example of One to Many relationship

      xml <Association Name="PublishedBy"> <End Type="BooksModel.Book" Role="Book" Multiplicity="*" /> <End Type="BooksModel.Publisher" Role="Publisher" Multiplicity="1" /> </Association>

  • Entities at one end of an association can be accessed through:
    • navigation properties
    • foreign key properties
  • Association instances are logically grouped in an association set
  • Association sets (and entity sets) are logically grouped in an entity container


  • Entity types contain properties that define their structure and characteristics


This is primary class that is responsible for interacting with data as objects

  • not released as part of the .NET Framework i.e. flexible and frequent with releasing.
  • recommended way to work with context is
    • Define a class that derives from DbContext
    • Exposes DbSet properties that represent collections of the specified entities in the context
  • Can do Add, Update and Delete

Entity Types


Stands for "plain-old" CLR objects

Dynamic Proxy

Entity Lifecycle


When retrieved from the database or Attached to DbContext


State isn't Tracked

  • default state of a newly created entity
  • entities retrieved from the database when tracking is disabled




Context Lifecycle

Best Practices:

  • Web Applications: Per Request to support Server Transections
  • Desktop Applications: Per Form/Dialog/Page