In this chapter, you will learn about Entity Framework Core and how it is an OR mapper for .NET (.NET Framework, .NET Core, Mono, and Xamarin). Entity Framework Core is a completely new implementation of the ADO.NET Entity Framework.
Together with .NET Core version 1.0 and ASP.NET Core version 1.0, Entity Framework Core version 1.0 was released on June 27, 2016. Version 2.0 was released on August 14, 2017. Version 2.1 is in progress.
What Is an Object-Relational Mapper?
In the database world, relational databases are prevalent. The programming world is all about objects. There are significant semantic and syntactic differences between the two worlds, called impedance mismatch ; see https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch .
Working with objects as instances of classes in memory is at the core of object-oriented programming (OOP) . Most applications also include the requirement to permanently store data in objects, especially in databases. Basically, there are object-oriented databases (OODBs) that are directly able to store objects, but OODBs have only a small distribution so far. Relational databases are more predominant, but they map the data structures differently than object models.
To make the handling of relational databases more natural in object-oriented systems, the software industry has been relying on object-relational mappers for years. These tools translate concepts from the object-oriented world, such as classes, attributes, or relationships between classes, to corresponding constructs of the relational world, such as tables, columns, and foreign keys (see Figure ). Developers can thus remain in the object-oriented world and instruct the OR mapper to load or store certain objects that are in the form of records in tables of the relational database. Less interesting tasks and error-prone ones, such as manually creating INSERT , UPDATE , and DELETE statements, are also handled by the OR mapper, further reducing the load on the developer.
Figure 1-1
The OR mapper translates constructs of the OOP world to the relational world
Two particularly striking differences between the object model and the relation model are N:M relations and inheritance. While in an object model you can map an N:M relationship between objects through a reciprocal set of objects, you need an intermediate table in the relational database. Relational databases do not support inheritance. There are different ways of replicating, but youll learn more about that later in the book.
OR Mappers in the .NET World
When a .NET developer reads in data from a database with a DataReader or DataSet , the developer is not doing object-relational mapping at this point. Although DataReader and DataSet are .NET objects, they only manage table structures. DataReader and DataSet are untyped, nonspecific containers from the point of view of an object model. Only when the developer defines specific classes for the structures stored in the tables and copies the contents from DataSet or DataReader into these specific data structures is the developer performing OR mapping. Such manual object-relational mapping is time-consuming, tedious, and monotonous programming work for read access (especially for very wide tables). If you then want to save changes in the objects again, the work becomes an intellectual challenge because you have to be able to recognize which objects have been changed. Otherwise, you constantly have to save all the data anew, which is an absurdity in multiuser environments.
While OR mappers have long been established in the Java world, Microsoft failed to bring a suitable product to market for a long time. The first version of .NET did not include an OR mapper but limited itself to direct data access and mapping between XML documents and the relational model. In .NET 3.5, there was an OR mapper named LINQ to SQL, but it was limited to Microsoft SQL Server and had many other restrictions.
Many .NET developers have therefore set about simplifying this work with auxiliary libraries and tools. In addition to the publicly known OR mappers for .NET, you will find many in-house solutions being built.
The following are third-party OR mappers for .NET (some of them open source):
With LINQ to SQL, ADO .NET Entity Framework, and Entity Framework, Microsoft itself now has three ORM products. The software company has meanwhile announced that further development efforts will concentrate solely on Entity Framework Core.
Version History of Entity Framework Core
Figure shows the version history of Entity Framework Core .
Figure 1-2
Entity Framework Core version history ( https://www.nuget.org/packages/Microsoft.EntityFrameworkCore )
Major and minor versions (1.0, 1.1, and so on) indicate feature releases from Microsoft, and revision releases (1.0.1, 1.0.2, and so on) indicate bug fix releases. This book mentions the minimum version when discussing a function that requires a particular version.
Note
The Entity Framework Core tools for Entity Framework Core 1. x were released on March 6, 2017, within the framework of Entity Framework Core 1.1.1 and Visual Studio 2017. Previously, there were only preview versions of the tools. Since Entity Framework Core 2.0, the tools are always delivered with the new product releases.
Supported Operating Systems
Like the other products in the Core product family, Entity Framework Core (formerly Entity Framework 7.0) is platform independent. The Core version of the established object-relational mapper runs not only on the full .NET Framework but also on .NET Core and Mono, including Xamarin. This allows you to use Entity Framework Core on Windows, Windows Phone/Mobile, Linux, macOS, iOS, and Android.
Supported .NET Versions
Entity Framework Core 1. x runs on .NET Core 1. x , .NET Framework 4.5.1, Mono 4.6, Xamarin.iOS 10, Xamarin Android 7.0 or higher, and the Universal Windows Platform (UWP).
Entity Framework Core 2.0 is based on .NET Standard 2.0 and therefore requires one of the following .NET implementations (see Figure ):
.NET Core 2.0 (or higher)
.NET Framework 4.6.1 (or higher)
Mono 5.4 (or higher)
Xamarin.iOS 10.14 (or higher)
Xamarin.Mac 3.8 (or higher)
Xamarin.Android 7.5 (or higher)
UWP 10.0.16299 (or higher)
Figure 1-3
Implementations of .NET Standard ( https://docs.microsoft.com/en-us/dotnet/standard/library )