Overview
The Entity Framework is a set of technologies in ADO.NET that supports the development of data-oriented software applications. Architects and developers of data-oriented applications have typically struggled with the need to achieve two very different objectives. They must model the entities, relationships, and logic of the business problems they are solving, and they must also work with the data engines used to store and retrieve the data. The data can span multiple storage systems, each with its own protocols; even applications that work with a single storage system must balance the requirements of the storage system against the requirements of writing efficient and maintainable application code. This problem is generally referred to as the "History
The first version of Entity Framework (EFv1) was included with .NET Framework 3.5 Service Pack 1 andArchitecture
The architecture of the ADO.NET Entity Framework, from the bottom up, consists of the following: * Data source specific providers, which abstract the ADO.NET interfaces to connect to the database when programming against the conceptual schema. * Map provider, a database-specific provider that translates the Entity SQL command tree into a query in the native SQL flavor of the database. It includes the Store-specific bridge, which is the component responsible for translating the generic command tree into a store-specific command tree. * EDM parser and view mapping, which takes the SDL specification of the data model and how it maps onto the underlying relational model and enables programming against the conceptual model. From the relational schema, it creates views of the data corresponding to the conceptual model. It aggregates information from multiple tables in order to aggregate them into an entity, and splits an update to an entity into multiple updates to whichever table(s) contributed to that entity. * Query and update pipeline, processes queries, filters and updates requests to convert them into canonical command trees which are then converted into store-specific queries by the map provider. * Metadata services, which handle all metadata related to entities, relationships and mappings. * Transactions, to integrate with transactional capabilities of the underlying store. If the underlying store does not support transactions, support for it needs to be implemented at this layer. * Conceptual layer API, the runtime that exposes the programming model for coding against the conceptual schema. It follows the ADO.NET pattern of using Connection objects to refer to the map provider, using Command objects to send the query, and returning EntityResultSets or EntitySets containing the result. * Disconnected components, which locally cache datasets and entity sets for using the ADO.NET Entity Framework in an occasionally connected environment. * Embedded database: ADO.NET Entity Framework includes a lightweight embedded database for client-side caching and querying of relational data. * Design tools, such as Mapping Designer, are also included with ADO.NET Entity Framework, which simplifies the job of mapping a conceptual schema to the relational schema and specifying which properties of an entity type correspond to which table in the database. * Programming layer, which exposes the EDM as programming constructs which can be consumed by programming languages. ** Object services, automatically generate code for CLR classes that expose the same properties as an entity, thus enabling instantiation of entities as .NET objects. ** Web services, which expose entities as web services. * High-level services, such as reporting services which work on entities rather than relational data.Entity Data Model
The Entity Data Model (EDM) specifies the conceptual model (CSDL) of the data, using a modelling technique that is itself called Entity Data Model, an extended version of the ''Mapping
Entity Data Model Wizard in Visual Studio initially generates a one-to-one (1:1) mapping between the database schema and the conceptual schema in most of the cases. In the relational schema, the elements are composed of the tables, with the primary and foreign keys gluing the related tables together. In contrast, the ''Entity Types'' define the conceptual schema of the data. The entity types are an aggregation of multiple typed fields – each field maps to a certain column in the database – and can contain information from multiple physical tables. The entity types can be related to each other, independent of the relationships in the physical schema. Related entities are also exposed similarly – via a field whose name denotes the relation they are participating in and accessing which, instead of retrieving the value from some column in the database, traverses the relationship and returns the entity (or a collection of entities) with which it is related. Entity Types form the class of objects entities conform to, with the Entities being instances of the entity types. Entities represent individual objects that form a part of the problem being solved by the application and are indexed by a key. For example, converting the physical schema described above, we will have two entity types: * ''CustomerEntity'', which contains the customer's name from the ''Customers'' table, and the customer's address from the ''Contacts'' table. * ''OrderEntity'', which encapsulates the orders of a certain customer, retrieving it from the ''Orders'' table. The logical schema and its mapping with the physical schema is represented as an Entity Data Model (EDM), specified as an XML file. ADO.NET Entity Framework uses the EDM to actually perform the mapping letting the application work with the entities, while internally abstracting the use of ADO.NET constructs like ''DataSet'' and ''RecordSet''. ADO.NET Entity Framework performs the ''Entities
Entities** are instances of ''EntityType''s; they represent the individual instances of the objects (such as ''customer'', ''orders'') to which the information pertains. The identity of an entity is defined by the entity type it is an instance of; in that sense an entity type defines the class an entity belongs to and also defines what ''properties'' an entity will have. Properties describe some aspect of the entity by giving it a name and a type. The properties of an entity type in ADO.NET Entity Framework are fully typed, and are fully compatible with the type system used in a DBMS system, as well as the Common Type System of the .NET Framework. A property can be ''SimpleType'', or ''ComplexType'', and can be multi-valued as well. All ''EntityType''s belong to some namespace, and have an ''EntityKey'' property that uniquely identifies each instance of the entity type. The different property types are distinguished as follows: * SimpleType, corresponds to primitive data types such as ''Integer'', ''Characters'' and ''Floating Point'' numbers. * ComplexType, is an aggregate of multiple properties of type ''SimpleType'', or ''ComplexType''. Unlike ''EntityType''s, however, ''ComplexTypes'' cannot have an ''EntityKey''. In Entity Framework v1 ''ComplexTypes'' cannot be inherited. All entity instances are housed in EntityContainers, which are per-project containers for entities. Each project has one or more named EntityContainers, which can reference entities across multiple namespaces and entity types. Multiple instances of one entity type can be stored in collections called EntitySets. One entity type can have multiple EntitySets. EDM primitive types (simple types):Relationships
Any two entity types can be related, by either an ''Association'' relation or a ''Containment'' relation. For example, a shipment ''is billed'' to a customer is an association whereas an order ''contains'' order details is a containment relation. A containment relation can also be used to model inheritance between entities. The relation between two entity types is specified by a ''Relationship Type'', instances of which, called ''Relationships'', relate entity instances. In future releases, other kinds of relationship types such as ''Composition'', or ''Identification'', may be introduced. Relationship types are characterized by their degree (arity) or the count of entity types they relate and their multiplicity. However, in the initial release of ADO.NET Entity Framework, relationships are limited to a binary (of degree two) bi-directional relationship. Multiplicity defines how many entity instances can be related together. Based on multiplicity, relationships can be either one-to-one, one-to-many, or many-to-many. Relationships between entities are named; the name is called a Role. It defines the purpose of the relationship. A relationship type can also have an ''Operation'' or ''Action'' associated with it, which allows some action to be performed on an entity in the event of an action being performed on a related entity. A relationship can be specified to take an ''Action'' when some ''Operation'' is done on a related entity. For example, on deleting an entity that forms the part of a relation (the ''OnDelete'' operation) the actions that can be taken are: * Cascade, which instructs to delete the relationship instance and all associated entity instances. * None. For association relationships, which can have different semantics at either ends, different actions can be specified for either end.Schema definition language
ADO.NET Entity Framework uses an XML based Data Definition Language called ''Schema Definition Language'' (SDL) to define the EDM Schema. The SDL defines the SimpleTypes similar to theQuerying data
Entity SQL
ADO.NET Entity Framework uses a variant of the SQL, Structured Query Language, named ''Entity SQL'', which is aimed at writing declarative queries and updates over entities and entity relationships – at the conceptual level. It differs from SQL in that it does not have explicit constructs for ''joins'' because the EDM is designed to abstract partitioning data across tables. Querying against the conceptual model is facilitated by ''EntityClient'' classes, which accepts an Entity SQL query. The query pipeline parses the Entity SQL query into a command tree, segregating the query across multiple tables, which is handed over to the EntityClient provider. Like ADO.NET data providers, an EntityClient provider is also initialized using a ''Connection'' object, which in addition to the usual parameters of data store and authentication info, requires the SDL schema and the mapping information. The EntityClient provider in turn then turns the Entity SQL command tree into an SQL query in the native flavor of the database. The execution of the query then returns an Entity SQL ResultSet, which is not limited to a tabular structure, unlike ADO.NET ResultSets. Entity SQL enhances SQL by adding intrinsic support for: * Types, as ADO.NET entities are fully typed. * EntitySets, which are treated as collections of entities. * Composability, which removes restrictions on where subqueries can be used.Entity SQL canonical functions
Canonical functions are supported by all Entity Framework compliant data providers. They can be used in an Entity SQL query. Also, most of the extension methods in LINQ to Entities are translated to canonical functions. They are independent of any specific database. When ADO.NET data provider receives a function, it translates it to the desired SQL statement. But not all DBMSs have equivalent functionality and a set of standard embedded functions. There are also differences in the accuracy of calculations. Therefore, not all canonical functions are supported for all databases, and not all canonical functions return the same results.LINQ to Entities
The LINQ to Entities provider allows LINQ to be used to query variousNative SQL
In the Entity Framework v4 new methods ''ExecuteStoreQuery()'' and ''ExecuteStoreCommand()'' were added to the class ObjectContext.Visualizers
Visual Studio has a feature called Visualizer. A LINQ query written in Visual Studio can be viewed as Native SQL using a Visualizer during debug session. A Visualizer for LINQ to Entities (Object Query) targeting all RDBMS is available in the Visual Studio Marketplace.Performance Profiling
Various profilers are commercially available to troubleshoot performance issues using Entity Framework, both for EF and EF Core variants.Tools and Extensions - Entity Framework Core
Entity Framework Core Tools and Extensions are available to enhance the performance of Entity Framework Core.Tools and extensions - Entity Framework EF6
Entity Framework Tools and Extensions are available to enhance the performance of Entity Framework.See also
*References
Bibliography
* * * *External links