Software:Entity Framework

From HandWiki
Entity Framework
Original author(s)Microsoft
Developer(s).NET Foundation
Initial releaseAugust 11, 2008; 17 years ago (2008-08-11)
Stable release
Entity Framework 6: v6.4.4
Entity Framework Core: v9.0.4 / Entity Framework Core: April 8, 2025; 10 months ago (2025-04-08)[1]
Repositorygithub.com/dotnet/ef6
github.com/dotnet/efcore
Written inC#
Platform.NET Framework,
.NET Core
TypeObject–relational mapping
LicenseApache License 2.0
Websitelearn.microsoft.com/en-us/ef/

Entity Framework (EF) is an open source[2] object–relational mapping (ORM) framework for ADO.NET. It was originally shipped as an integral part of .NET Framework, however starting with Entity Framework version 6.0 it has been delivered separately from the .NET Framework.

Entity Framework 6.4 was the latest release of the classic framework. Although Entity Framework 6 is still supported, it is no longer being developed and will only receive fixes for security issues.[3]

A new framework known as Entity Framework Core (EF Core) was introduced in 2016 with similar but not complete feature parity.[4] Version numbering of this framework restarted from 1.0 and the latest version of EF Core is 9.0.4.[5]

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 "object–relational impedance mismatch".[6]

Many object–relational mapping (ORM) tools (aka "object–relational managers") have been developed to enable 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 an ORM, 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. Entity Framework is the ORM solution currently promoted for use within the Microsoft development stack.[7]

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 (17 years ago) (2008-08-11). This version was widely criticized, even attracting a 'vote of no confidence' signed by at least one thousand developers.[8]

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.[9]

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.[10] There were a few updates, like support for migration.

Version 5.0.0 was released on August 11, 2012[11] 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[12] 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.[13] This version has a number of improvements for code-first support.[14]

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.[15] On 27 June 2016 this was released as Entity Framework Core 1.0, alongside ASP.NET Core 1.0 and .NET Core 1.0.[16] 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.[17]

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

Entity Framework Core 2.0 was released on 14 August 2017 (8 years ago) (2017-08-14) along with Visual Studio 2017 15.3 and ASP.NET Core 2.0 [18]

Entity Framework Core 3.0 was released on 23 September 2019 (6 years ago) (2019-09-23) along with Visual Studio 2019 16.3 and ASP.NET Core 3.0,[19]

Entity Framework Core 3.1 (EF Core 3.1) was formally released for production use on 3 December 2019 (6 years ago) (2019-12-03) and will be the preferred long-term supported version until at least 3 December 2022.[20][21]

Entity Framework Core 5.0 (EF Core 5) was released for production use on 9 November 2020 (5 years ago) (2020-11-09).[20][21] It was retired and out of support 1.5 years later on May 10, 2022.[22]

Entity Framework Core 6.0 (EF Core 6) was released on 10 November 2021 (4 years ago) (2021-11-10)[23][24] and will be the preferred long-term supported version until at least 12 November 2024.[25]

Entity Framework Core 7.0 (EF Core 7) was released on 8 November 2022 (3 years ago) (2022-11-08)[26][27] adding features such as JSON columns and bulk updates.

Entity Framework Core 8.0 (EF Core 8) was released on 14 November 2023 (2 years ago) (2023-11-14)[28][29] adding features such as Value objects using Complex Types and Primitive collections.

Architecture

ADO.NET Entity Framework stack.
  • 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.[30] 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.[31]

An Entity Model created using Entity Framework 6 which represents Customer, Order and Product tables

Visual Studio also provides the Entity Designer for visual creation of the EDM and the mapping specification. This approach is named as "Model First" approach, as alternatives to "Code First" and "Database First" approaches. 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. The "Model First" approach is not going to be supported in EF Core version. [32]

Mapping

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.

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.[33]

Entities

  • SimpleType, corresponds to primitive data types such as Integer, Characters and Floating Point numbers.[34]
  • 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.[35]


EDM primitive types (simple types):[34][36]

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

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

  • Cascade, which instructs to delete the relationship instance and all associated entity instances.
  • None.


Schema definition language

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

<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

  • 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.[39]


Group Canonical functions[39]
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

Native SQL

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.[40]

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.[41]

EF Core exposes design-time commands for migrations and reverse engineering via the .NET CLI (dotnet ef) and Visual Studio Package Manager Console, which scaffold a DbContext and entity types from an existing database. Beyond the official tooling, IDE integrations provide GUI workflows for common tasks: EF Core Power Tools in Visual Studio adds database-first reverse engineering and model visualization, while Entity Developer offers a visual designer for EF Core that supports database-first and model-first approaches with code generation and mapping configuration. Similar capabilities are available in other IDEs (e.g., Rider’s UI for EF Core commands), and advanced scenarios can leverage customizable scaffolding templates to control generated code.

Tools and extensions - Entity Framework EF6

Entity Framework Tools and Extensions are available to enhance the performance of Entity Framework.[42]

See also

References

  1. "Releases · dotnet/efcore · GitHub". https://github.com/dotnet/efcore/releases. 
  2. Krill, Paul (20 July 2012). "Microsoft open-sources Entity Framework". InfoWorld. http://www.infoworld.com/d/application-development/microsoft-open-sources-entity-framework-198213. 
  3. ajcvickers (9 March 2022). "Entity Framework Support Policies" (in en-us). https://learn.microsoft.com/en-us/ef/efcore-and-ef6/support. 
  4. ajcvickers. "Compare EF6 and EF Core" (in en-us). https://learn.microsoft.com/en-us/ef/efcore-and-ef6/. 
  5. "Releases · dotnet/efcore". https://github.com/dotnet/efcore/releases. 
  6. "The Object-Relational Impedance Mismatch" (in en). http://www.agiledata.org/essays/impedanceMismatch.html. 
  7. mcleblanc (15 September 2021). "Entity Framework Overview - ADO.NET" (in en-us). https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/overview. 
  8. "Entity Framework Vote of No Confidence Signatories | Wufoo". https://efvote.wufoo.com/reports/entity-framework-vote-of-no-confidence-signatories/#public. 
  9. "Update on the Entity Framework in .NET 4 and Visual Studio 2010". ADO.NET team blog. May 11, 2009. http://blogs.msdn.com/adonet/archive/2009/05/11/update-on-the-entity-framework-in-net-4-and-visual-studio-2010.aspx. 
  10. "EF4.3.1 and EF5 Beta 1 Available on NuGet". ADO.NET team blog. February 29, 2012. http://blogs.msdn.com/b/adonet/archive/2012/02/29/ef4-3-1-and-ef5-beta-1-available-on-nuget.aspx. 
  11. "EF5 Available on CodePlex". August 11, 2012. https://entityframework.codeplex.com/releases/view/86738. 
  12. "EF6 RTM Available". October 17, 2013. http://blogs.msdn.com/b/adonet/archive/2013/10/17/ef6-rtm-available.aspx. 
  13. "Entity Framework - Home". September 14, 2016. https://github.com/aspnet/EntityFramework6. 
  14. ajcvickers (14 October 2020). "What's new - EF6" (in en-us). https://learn.microsoft.com/en-us/ef/ef6/what-is-new/. 
  15. 15.0 15.1 "EF7 - New Platforms, New Data Stores". May 19, 2014. http://blogs.msdn.com/b/adonet/archive/2014/05/19/ef7-new-platforms-new-data-stores.aspx. 
  16. "Entity Framework Core 1.0.0 Available". 27 June 2016. https://blogs.msdn.microsoft.com/dotnet/2016/06/27/entity-framework-core-1-0-0-available/. 
  17. Hanselman, Scott. "ASP.NET 5 is dead - Introducing ASP.NET Core 1.0 and .NET Core 1.0 - Scott Hanselman". http://www.hanselman.com/blog/ASPNET5IsDeadIntroducingASPNETCore10AndNETCore10.aspx. 
  18. "Announcing .NET Core 2.0". .NET Blog. 14 August 2017. https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-net-core-2-0/. 
  19. "Announcing .NET Core 3.0". .NET Blog. 23 September 2019. https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0/. 
  20. 20.0 20.1 ajcvickers. "Plan for Entity Framework Core 5.0" (in en-us). https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/plan. 
  21. 21.0 21.1 "Microsoft.EntityFrameworkCore 5.0.0" (in en). https://nuget.org/packages/Microsoft.EntityFrameworkCore/. 
  22. "Entity Framework Core Releases and Planning". December 2023. https://learn.microsoft.com/en-us/ef/core/what-is-new/. 
  23. ajcvickers. "Plan for Entity Framework Core 6.0" (in en-us). https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/plan. 
  24. "Microsoft.EntityFrameworkCore 6.0.0" (in en). https://nuget.org/packages/Microsoft.EntityFrameworkCore/. 
  25. ajcvickers. "EF Core releases and planning" (in en-us). https://learn.microsoft.com/en-us/ef/core/what-is-new/. 
  26. Roth, Daniel (2022-11-08). "Announcing ASP.NET Core in .NET 7" (in en-US). https://devblogs.microsoft.com/dotnet/announcing-asp-net-core-in-dotnet-7/. 
  27. ajcvickers. "What's New in EF Core 7.0" (in en-us). https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew. 
  28. Arthur, Vickers (2023-11-14). "Entity Framework Core 8 (EF8) is available today" (in en-US). https://devblogs.microsoft.com/dotnet/announcing-ef8/. 
  29. ajcvickers (14 November 2023). "What's New in EF Core 8" (in en-us). https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew. 
  30. "Entity Data Model". MSDN, Microsoft. August 2, 2012. http://msdn.microsoft.com/en-us/library/ee382825.aspx. 
  31. CSDL, SSDL, and MSL Specifications, MSDN, http://msdn.microsoft.com/en-us/library/bb399604.aspx, retrieved 2010-12-06 
  32. ajcvickers (2022-03-09). "Porting from EF6 to EF Core - Porting an EDMX-Based Model - EF" (in en-us). https://learn.microsoft.com/en-us/ef/efcore-and-ef6/porting/port-edmx. 
  33. Kogent Solutions Inc. (2009), ASP.NET 3.5 Black Book, Dreamtech Press, ISBN 978-81-7722-831-1 
  34. 34.0 34.1 Simple Types (EDM), MSDN, http://msdn.microsoft.com/en-us/library/bb399213(VS.100).aspx, retrieved 2010-12-06 
  35. ComplexType Element (CSDL), MSDN, http://msdn.microsoft.com/en-us/library/cc716799(VS.100).aspx, retrieved 2010-12-06 
  36. Conceptual Model Types, MSDN, http://msdn.microsoft.com/en-us/library/bb399548(VS.100).aspx, retrieved 2010-12-06 
  37. OnDelete Element (CSDL), MSDN, http://msdn.microsoft.com/en-us/library/cc716734(VS.100).aspx, retrieved 2010-12-06 
  38. Facets (CSDL), MSDN, http://msdn.microsoft.com/en-us/library/cc716737(VS.100).aspx, retrieved 2010-12-06 
  39. 39.0 39.1 Canonical Functions (Entity SQL), MSDN, http://msdn.microsoft.com/en-us/library/bb738626(VS.100).aspx, retrieved 2010-03-29 
  40. "Linq to Entity query visualizer - Visual Studio Marketplace" (in en-us). https://marketplace.visualstudio.com/items?itemName=RRV.LinqtoEntityqueryvisualizer. 
  41. ErikEJ. "Tools & Extensions - EF Core" (in en-us). https://learn.microsoft.com/en-us/ef/core/extensions/. 
  42. ajcvickers (9 March 2022). "Tools & Extensions - EF6" (in en-us). https://learn.microsoft.com/en-us/ef/ef6/resources/tools. 
Cite error: <ref> tag with name "EdmWizard" defined in <references> is not used in prior text.

Bibliography