Author - StudySection Post Views - 335 views

Entity Framework introduction in .NET

Entity Framework is described as an ORM (Object Relational Mapping) framework that provides an automatic mechanism for developers to store and access the information from the database.

It is the primary source of interaction between .NET applications and relational databases. Entity Framework consists of a variety of tools that simplify mapping between objects in your software to the tables and columns of a computer database. ORM automatically creates classes supported database tables or vice-versa i.e. it can even automatically generate necessary SQL to form database tables supported classes.

Use of Entity Framework

  • It is to extend the developer’s productivity by reducing the redundant task of persisting the info used in the applications.
  • Entity Framework can generate the required database commands for reading or writing data within the database and execute them.
  • If you’re querying, you’ll be able to express your queries against your domain objects using LINQ to entities.
  • It will execute the relevant query within the database to materialize results into instances of your domain objects for you to figure within your app.

The architecture of Framework consists of the following:

  • Data Providers:
    Data providers are source-specific, that abstract interfaces are attached to connect the database when programming against the conceptual schema. It translates SQL languages like LINQ by command tree to the native System Query Language expressions and then executes them against the particular DBMS system.
  • Entity Client:
    It is a data provider used by entity framework applications to access the data, this layer exposes the entity layer to the upper layer. Entity client provides the flexibility for developers to figure against entities within the style of rows and columns using entity SQL queries without the necessity to get classes to represent conceptual schema. Entity Client represents the core functionality layers of the entity framework. The following layers are called Entity Data Model.

    • The Storage Layer contains the complete database schema in XML format.
    • The Entity Layer which is additionally an XML file defines the entities and relationships.
    • The mapping layer is an XML-type file that maps the relationships and entities defined at the conceptual layer with actual relationships and tables defined at the logical layer.
    • Metadata services are presented in the Entity Client that provides a centralized API that accesses the metadata stored in Entity, Mapping, and Storage layers.
  • Object Service:
    The Object Services enables to work with entities such as memory objects. It is the Object Context, that represents the session of interaction between the data source and applications.

    • Article Context is used to perform different operations like add, delete instances of entities, and save lots of the changed states back to the database with the assistance of queries.
    • ORM layer shows the result to the instances of entities.
    • It allows developers to use various ORM features like primary key mapping, change tracking, etc. by writing queries using Entity SQL or LINQ.

Entity Data Model:

The Entity Data Model (EDM) specifies (CSDL) the conceptual model and an extended version entity-relationship model. It also refers to a collection of concepts that describe the data structure of the system, no matter its stored form.
The model uses three core parts of EDM.

  • Storage Schema Model:
    The Storage Model is a database design which is also called SSDL (Storage Schema Definition Layer) which represents a schematic representation of backend data.
  • Conceptual Model:
    The Conceptual Model contains the model classes and is also called Conceptual Schema Definition Layer (CSDL) is that the real entity model, against which we write our queries.
  • Mapping Model:
    The mapping layer consists of information that is simply a mapping between the Conceptual model and also the Storage model. Mapping of logical and physical schema is represented as an EDM(Entity Data Model).

Schema Definition Language:

The framework uses an XML-based DDL called Schema Definition Language (SDL) to define Schema.

  • The SDL defines types like Int32, Double, Decimal, String, and DateTime, among others in a similar way.
  • Enumeration, a map of names and primitive values, is also considered a Simple Type. They are supported from framework version 5.0 onwards only.
  • The types that are being created from an aggregation of other types are Complex Types. A group of properties of these types is an Entity Type.

The data model has mainly 3 key concepts:

  • Entity Type
    • It is a fundamental building block to describe the structure.
    • In the conceptual model, entity types are being constructed from the properties and it describes the structure of top-level concepts.
    • It represents an object which is selected.
    • Each of the entities should have a novel entity key in the entity set. This set can be a collection of instances that are of a particular type. The association sets and entity sets are grouped in an entity container logically.
  • Association Type
    • It describes a relationship between two entities and is another fundamental building block in EDM for describing relationships during a conceptual model.
    • Every association has two association ends that specify the entity types involved within the association.
    • Accordingly, each association end also specifies an association end multiplicity that describes the number of entities that may be at that end of the association.
    • An association end multiplicity can have a worth of 1 (1), zero or one (0..1), or many (*).
    • Entities at one end of an association may be accessed through navigation properties, or foreign keys if they’re exposed on an entity type.
  • Property
    • Property defines the structure and characteristics. For example, a student entity type may have properties such as student name and student id.
    • A property can contain primary data (such as an integer, a string, or a Boolean value), or structured data (such as a fancy type).

The Entity Framework entitles you to insert, question, delete, and update data, using Common Language Runtime (CLR) objects that can be referred to as entities. The Entity Framework maps the entities and relationships that are defined in your model to a database. It also provides resources to

  • Transpire data returned from the database as entity objects
  • Track changes made to the objects
  • Handle concurrency
  • Propagate object changes back to the database
  • Bind objects to controls

The primary class that is liable for interacting with data as objects is System.Data.Entity.DbContext.

  • DbContext API is similar to DbContext that represents a combination of the Unit Of Work and Repository patterns it can be also be used to query from a database and group together the changes which will be written back to the store as a single unit.
  • It also reduces the number of relationships and represents properties you wish to access in tasks.
  • The context class manages the entity objects during run time, which has populating objects with data from a database, change tracking, and persisting data to the database.


There are three varieties of queries –

  • Adding New Entities
    Adding a brand new object with Entity Framework is elementary with constructing a new case of the object and registering it using the Add method on DbSet.
  • Changing Existing Entities
    Changing existing objects is simple for updating the worth assigned to the property(s) could be like to be changed and calling Save Changes.
  • Deleting Existing Entities
    For deleting an entity using Entity Framework, we can employ the Remove method on DbSet. It removes the work for both existing and newly added entities. Calling Remove on an entity that has been added but not yet saved to the database will cancel the addition of the entity. The entity is off from the change tracker and isn’t any longer tracked by the DbContext.

Types Of Entities

In Entity Framework, there are two types of entities in entity framework: POCO entities and Dynamic Proxy
POCO Entities:

  • POCO stands for “plain-old CLR objects” which doesn’t depend on any framework-specific base class that might be used as existing domain objects together with the data model.
  • POCO data classes that are mapped to entities are defined during a data model.
  • It also supports most of the identical insert, query, deletes, and update behaviours as entity types that are generated by the Entity Data Model tools.
  • POCO template can be used to get persistence-ignorant entity types from a conceptual model.

Dynamic Proxy

  • When we create an instance of POCO entity type, the Framework automatically creates an instance of the dynamically generated derived type which acts as a proxy for the above-mentioned entity. It is a runtime proxy class sort of a wrapper class of POCO entities. Some properties of the entity for performing actions automatically when the property is accessed can be overridden.
  • This also supports the lazy loading of the defined relationships and the automatic change tracking.
  • It applies to the models which are created with Code First and EF Designer approaches.

To support the lazy loading of the defined related objects & also to trace the changes in POCO classes, then

  • The custom data classes should have public access declaration.
  • The data class should not be abstract or sealed.
  • Data class should also have a public or protected parameterless constructor which can hold the CreateObject method to create a proxy for the entity.
  • Only calling the CreateObject method does not create the proxy, the class should follow all the requirements.
  • The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces as these are implemented by the proxy classes.
  • The ProxyCreationEnabled option should be enabled that is, it should be set to true.
  • To disable the creation of proxy objects, the value of ProxyCreationEnabled property should be set to false.


In RDBMS or relational databases, the relation is said to exist between different tables through the defined foreign keys. There are mainly 3 forms of relationships between tables which also depend on how the related columns are being constructed. In the framework, these relationships can also be created through code.
One-to-Many Relationship
Many-to-Many Relationship
One-to-One Relationship

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked * cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri