In our Introduction to Entity Framework tutorial, we looked at the Microsoft Entity Framework. In this Getting Started With Entity Framework tutorial, we will look at the Basic building blocks of the entity framework.
Before Using the Entity framework, you need to know about the various terms used in the entity framework.
Table of Contents
The entity is defined as an object with independent existence. For example, an employee in working for a company is an entity. Similarly, Department in a company is an entity. From the perspective of a database, the entity represents the row in the database table.
Each Entity has properties or attributes similar to a class. For example, in the case of employee entity, his name, joining date and address are Properties. The Properties can be int, string, collections or of a complex type. From the database, perspective Properties are represented as columns.
The Entity type is a collection of entities that share the common properties. It represents a unique object in your domain model. The Entity is a single instance of Entity Type. From the database perspective, the entity type is your table. From the application developer’s perspective, the entity type is a domain class.
The Relationship between two entity types is known as the Association. Association can be one to one, one to many or many to many. For Example, an Employee can be part of one department, which is a one to one association. The Employee can handle multiple projects which is a one to many associations. From the database, perspective relationship is expressed as Primary key – Foreign key
EntityKey is similar to a Primary key. Every entityType must define an entityKey which uniquely identifies the individual instance of an entity
An entity set is a set of entities of the same Entity type. In OOP entity set is a collection of objects. Entity type and entity sets are somewhat analogous.
Entity Data Model
Entity Data Model (EDM) is the heart of our entity framework. The Entity Data model describes the Conceptual model of our domain objects. It enables you to create strongly typed domain classes (Entity types). It defines associations between these domain classes. It then maps these classes to the database Schema.
The above image shows the architecture of EDM. The Entity data model can be designed with a visual designer (Database First or model first) or with the Code (Code First).
Entity Data Model Layers
As shown in the above image, the Entity Data model consists of three separate layers. The conceptual model, Storage model and a Mapping layer. Each layer is decoupled from each other.
The conceptual model describes how the model looks from the application’s perspective. It contains our domain model classes for Entity Types. Your Application interacts with the Conceptual model.
The Responsibility of the storage model is to persist the data to the data stores. It includes the definition of tables, views, Indexes, stored procedures, and their relationships and keys which map to the underlying database.
Mapping layer maps the conceptual model of the storage model. It sits between Conceptual model and Storage model. IT defines how to conceptual model needs to be mapped to the database table.
Entity Framework Architecture
The above shows image shows the architecture of entity framework. It includes the Entity Data Model (EDM) , the layer of accessing the data, Object Services, an Entity Client data provider and ADO.NET data provider.
Accessing the Data
>In the EF you don’t access the database. You will query the conceptual data model for the data. You can do this using LINQ to Entities or Entity SQL
LINQ to Entities
LINQ to Entities is a popular query language which enables us to write queries against the conceptual model. It returns entities
Entity SQL is another query language that is used.
Object Services is a layer that exposes database data as regular .NET objects. This takes the data reader returned by the >Entity Client Data Provider and converts the data into entities. This process called materialization.
Entity Client Data Provider
This Entity Client Data Provider allows the applications to query the data from the database. It Manages database connections. Translates entity queries into database specific queries. The Entity client data provider returns the datareader from the database. The Entity Client data provider also exposes standard ADO.NET Data Provider to the applications to execute Entity SQL queries.
ADO.Net Data Provider
This is standard ADO.Net.data Provider
In the next tutorial, we will look at the code first workflow of Entity Framework.