Entity Framework

This is an old revision of this page, as edited by Ghettoblaster (talk | contribs) at 21:26, 17 September 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Entity Framework (EF) is an open source[3] object-relational mapping (ORM) framework for ADO.NET. It was a part of .NET Framework, but since Entity framework version 6 it is separated from .NET framework.

Entity Framework
Developer(s)Microsoft
Initial releaseAugust 11, 2008; 16 years ago (2008-08-11)
Stable release
6.2.0 / October 26, 2017; 7 years ago (2017-10-26)
EF Core 2.2.6 / July 9, 2019; 5 years ago (2019-07-09)[1]
Preview release
6.3.0-preview8-19405-04 / August 13, 2019; 5 years ago (2019-08-13)[2]
EF Core 3.0.0-preview8 / August 13, 2019; 5 years ago (2019-08-13)[1]
Repository
Written inC#
Platform.NET Framework
TypeObject-relational mapping
LicenseApache License
Websitemsdn.microsoft.com/en-us/data/ef.aspx

Overview

The Entity Framework is a set of technologies in ADO.NET that support 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 may 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.

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications. [4]

History

The first version of Entity Framework (EFv1) was included with .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1, released on 11 August 2008. This version was widely criticized, even attracting a 'vote of no confidence' signed by approximately one thousand developers.[5]

The second version of Entity Framework, named Entity Framework 4.0 (EFv4), was released as part of .NET 4.0 on 12 April 2010 and addressed many of the criticisms made of version 1.[6]

A third version of Entity Framework, version 4.1, was released on April 12, 2011, with Code First support.

A refresh of version 4.1, named Entity Framework 4.1 Update 1, was released on July 25, 2011. It includes bug fixes and new supported types.

The version 4.3.1 was released on February 29, 2012.[7] There were a few updates, like support for migration.

Version 5.0.0 was released on August 11, 2012[8] and is targeted at .NET framework 4.5. Also, this version is available for .Net framework 4, but without any runtime advantages over version 4.

Version 6.0 was released on October 17, 2013[9] and is now an open source project licensed under Apache License v2. Like ASP.NET MVC, its source code is hosted at GitHub using Git.[10] This version has a number of improvements for code-first support.[11]

Microsoft then decided to modernize, componentize and bring .NET cross-platform to Linux, OSX and elsewhere, meaning the next version of Entity Framework would be a complete rewrite.[12] On 27 June 2016 this was released as Entity Framework Core 1.0, alongside ASP.Net Core 1.0 and .Net Core 1.0.[13] It was originally named Entity Framework 7, but was renamed to highlight that it was a complete rewrite rather than an incremental upgrade and it doesn't replace EF6.[14]

EF Core 1.0 is licensed under Apache License v2, and is being built entirely in the open on GitHub. While EF Core 1.0 shares some conceptual similarities with prior versions of Entity Framework, it is a completely new codebase designed to be more efficient, powerful, flexible, and extensible, will run on Windows, Linux and OSX, and will support a new range of relational and NOSQL data stores.[12]

EF Core 2.0 was released on 14 August 2017 along with Visual Studio 2017 15.3 and ASP.NET Core 2.0 [15]

Architecture

 
ADO.NET Entity Framework stack

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 Entity-Relationship model.[16] The data model primarily describes the Entities and the Associations they participate in. The EDM schema is expressed in the Schema Definition Language (SDL), which is an application of XML (Extended markup language). In addition, the mapping (MSL) of the elements of the conceptual schema (CSDL) to the storage schema (SSDL) must also be specified. The mapping specification is also expressed in XML.[17]

Visual Studio also provides the Entity Designer for visual creation of the EDM and the mapping specification. The output of the tool is the XML file (*.edmx) specifying the schema and the mapping. Edmx file contains EF metadata artifacts (CSDL/MSL/SSDL content). These three files (csdl, msl, ssdl) can also be created or edited by hand.

Mapping

Entity Data Model Wizard[18] 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) it is related with.

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 joins necessary to have entity reference information from multiple tables, or when a relationship is traversed. When an entity is updated, it traces back which table the information came from and issues SQL update statements to update the tables in which some data has been updated. ADO.NET Entity Framework uses eSQL, a derivative of SQL, to perform queries, set-theoretic operations, and updates on entities and their relationships. Queries in eSQL, if required, are then translated to the native SQL flavor of the underlying database.

Entity types and entity sets just form the logical EDM schema, and can be exposed as anything. ADO.NET Entity Framework includes Object Service that presents these entities as Objects with the elements and relationships exposed as properties. Thus Entity objects are just front-end to the instances of the EDM entity types, which lets Object Oriented languages access and use them. Similarly, other front-ends can be created, which expose the entities via web services (e.g., WCF Data Services) or XML that is used when entities are serialized for persistence storage or over-the-wire transfer.[19]

Entities

Entities** are instances of EntityTypes; 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 EntityTypes 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.[20]
  • ComplexType, is an aggregate of multiple properties of type SimpleType, or ComplexType. Unlike EntityTypes, however, ComplexTypes cannot have an EntityKey. In Entity Framework v1 ComplexTypes cannot be inherited.[21]

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):[20][22]

EDM type CLR type mapping
Edm.Binary Byte[]
Edm.Boolean Boolean
Edm.Byte Byte
Edm.DateTime DateTime
Edm.DateTimeOffset DateTimeOffset
Edm.Decimal Decimal
Edm.Double Double
Edm.Guid Guid
Edm.Int16 Int16
Edm.Int32 Int32
Edm.Int64 Int64
Edm.SByte SByte
Edm.Single Single
Edm.String String
Edm.Time TimeSpan

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:[23]

  • 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 the CTS primitive types, including String, Int32, Double, Decimal, Guid, and DateTime, among others. An Enumeration, which defines a map of primitive values and names, is also considered a simple type. Enumerations are supported from framework version 5.0 onwards only. ComplexTypes are created from an aggregation of other types. A collection of properties of these types define an Entity Type. This definition can be written in EBNF grammar as:

  EntityType ::= ENTITYTYPE entityTypeName [BASE entityTypeName]
    [ABSTRACT true|false] KEY propertyName [, propertyName]*
      {(propertyName PropertyType [PropertyFacet]*) +}
  PropertyType ::= ((PrimitiveType [PrimitiveTypeFacets]*)
    |  (complexTypeName) | RowType
  PropertyFacet ::= ( [NULLABLE true | false] |
    [DEFAULT defaultVal] | [MULTIPLICITY [ 1|*] ] )
  PropertyTypeFacet ::= MAXLENGTH | PRECISION | SCALE | UNICODE | FIXEDLENGTH | COLLATION
    | DATETIMEKIND | PRESERVESECONDS
  PrimitiveType ::= BINARY | STRING | BOOLEAN
    | SINGLE | DOUBLE | DECIMAL | GUID
    | BYTE | SBYTE | INT16 | INT32 | INT64
    | DATETIME | DATETIMEOFFSET | TIME )

Facets[24] are used to describe metadata of a property, such as whether it is nullable or has a default value, as also the cardinality of the property, i.e., whether the property is single valued or multi valued. A multiplicity of “1” denotes a single valued property; a “*” means it is a multi-valued property. As an example, an entity can be denoted in SDL as:

<ComplexType Name="Addr">
    <Property Name="Street" Type="String" Nullable="false" />
    <Property Name="City" Type="String" Nullable="false" />
    <Property Name="Country" Type="String" Nullable="false" />
    <Property Name="PostalCode" Type="Int32" />
</ComplexType>
<EntityType Name="Customer">
    <Key>
        <PropertyRef Name="Email" />
    </Key>
    <Property Name="Name" Type="String" />
    <Property Name="Email" Type="String" Nullable="false" />
    <Property Name="Address" Type="Addr" />
</EntityType>

A relationship type is defined as specifying the end points and their multiplicities. For example, a one-to-many relationship between Customer and Orders can be defined as

<Association Name="CustomerAndOrders">
    <End Type="Customer" Multiplicity="1" />
    <End Type="Orders" Multiplicity="*">
        <OnDelete Action="Cascade" />
    </End>
</Association>

Querying data

Entity SQL

ADO.NET Entity Framework uses a variant of the 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.[25]

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.

Group Canonical functions[25]
Aggregate functions Avg, BigCount, Count, Max, Min, StDev, StDevP, Sum, Var, VarP
Math functions Abs, Ceiling, Floor, Power, Round, Truncate
String functions Concat, Contains, EndsWith, IndexOf, Left, Length, LTrim, Replace, Reverse, Right, RTrim, Substring, StartsWith, ToLower, ToUpper, Trim
Date and Time functions AddMicroseconds, AddMilliseconds, AddSeconds, AddMinutes, AddHours, AddNanoseconds, AddDays, AddYears, CreateDateTime, AddMonths, CreateDateTimeOffset, CreateTime, CurrentDateTime, CurrentDateTimeOffset, CurrentUtcDateTime, Day, DayOfYear, DiffNanoseconds, DiffMilliseconds, DiffMicroseconds, DiffSeconds, DiffMinutes, DiffHours, DiffDays, DiffMonths, DiffYears, GetTotalOffsetMinutes, Hour, Millisecond, Minute, Month, Second, TruncateTime, Year
Bitwise functions BitWiseAnd, BitWiseNot, BitWiseOr, BitWiseXor
Other functions NewGuid

LINQ to Entities

The LINQ to Entities provider allows LINQ to be used to query various RDBMS data sources. Several database server specific providers with Entity Framework support are available.

Native 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 via VisualStudioGallery.

See also

References

  1. ^ a b "Releases · aspnet/EntityFrameworkCore · GitHub".
  2. ^ "Releases · aspnet/EntityFramework6 · GitHub".
  3. ^ Krill, Paul (20 July 2012). "Microsoft open-sources Entity Framework". InfoWorld. Retrieved 24 July 2012.
  4. ^ https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/overview
  5. ^ ADO .NET Entity Framework Vote of No Confidence
  6. ^ "Update on the Entity Framework in .NET 4 and Visual Studio 2010". ADO.NET team blog. May 11, 2009. Archived from the original on January 20, 2010. Retrieved November 1, 2011.
  7. ^ "EF4.3.1 and EF5 Beta 1 Available on NuGet". ADO.NET team blog. February 29, 2012. Archived from the original on March 25, 2012. Retrieved March 27, 2012.
  8. ^ "EF5 Available on CodePlex". August 11, 2012.
  9. ^ "EF6 RTM Available". October 17, 2013. Archived from the original on 2014-03-30.
  10. ^ "Entity Framework - Home". September 14, 2016.
  11. ^ "EF Version History".
  12. ^ a b "EF7 - New Platforms, New Data Stores". May 19, 2014. Archived from the original on 2015-09-29.
  13. ^ "Entity Framework Core 1.0.0 Available". 27 June 2016.
  14. ^ Hanselman, Scott. "ASP.NET 5 is dead - Introducing ASP.NET Core 1.0 and .NET Core 1.0 - Scott Hanselman". www.hanselman.com. Retrieved 2016-07-11.
  15. ^ "Announcing .NET Core 2.0". .NET Blog. 14 August 2017.
  16. ^ "Entity Data Model". MSDN, Microsoft. August 2, 2012. Retrieved August 15, 2013.
  17. ^ CSDL, SSDL, and MSL Specifications, MSDN, archived from the original on 8 November 2010, retrieved December 6, 2010
  18. ^ Entity Data Model Wizard, MSDN, retrieved December 6, 2010
  19. ^ Kogent Solutions Inc. (2009), ASP.NET 3.5 Black Book, Dreamtech Press, ISBN 81-7722-831-5
  20. ^ a b Simple Types (EDM), MSDN, retrieved December 6, 2010
  21. ^ ComplexType Element (CSDL), MSDN, retrieved December 6, 2010
  22. ^ Conceptual Model Types, MSDN, retrieved December 6, 2010
  23. ^ OnDelete Element (CSDL), MSDN, retrieved December 6, 2010
  24. ^ Facets (CSDL), MSDN, retrieved December 6, 2010
  25. ^ a b Canonical Functions (Entity SQL), MSDN, retrieved March 29, 2010

Further reading

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy