Entity Framework Corealso known as EF Coreis an object-relational mapping (ORM) package produced by Microsoft that allows .NET Core applications to store data in relational databases.
Understanding Entity Framework Core
Entity Framework Core has one key task: storing.NET objects in a database and retrieving them again later. Put another way, Entity Framework Core acts as the bridge between an ASP.NET Core MVC application and a database, as shown in Figure .
Figure 1-1.
Entity Framework Core in context
Storing .NET objects in a database can be a surprisingly complex process. Databases dont exist in isolation. They are created and managed by database servers , which are specialized applications focused solely on the storage and management of data. Database servers provide the persistent storage for most applications, and the most popular database servers have been under development for decades, resulting in high-performance and robust software that is packed with features. Database servers support a core set of common features, but they differentiate themselves with custom additions, and getting the best results from a database server means taking advantage of them.
There are different types of database server available; the kind that Entity Framework Core works with is called a relational database server , also known as a relational database management system (RDBMS). A relational database server manages relational databases , where data is stored as rows in tables, not unlike a spreadsheet. Relational database servers typically accept commands expressed in the Structured Query Language ( SQL) , which allows data operationssuch as storing or deleting datato be expressed. There is a SQL standard, but database servers use slightly different dialects, especially when it comes to accessing the nonstandard features.
Note
I am simplifying here for the sake of brevity. The world of databases is filled with distinctions between different types of data and different types of databases and database servers, none of which has much bearing on using ASP.NET Core MVC or Entity Framework Core.
To store .NET objects in a database, Entity Framework Core has to be able to translate objects into a form that can be stored in a database table and formulate a SQL command that the database server can process. To deal with the differences between database servers, Entity Framework Core relies on a database provider , which is responsible for communicating with the database and formulating the SQL commands.
To retrieve .NET objects from the database, Entity Framework Core has to be able to reverse the process. It has to be able to create a SQL query that will ask the database server for the data values that represent the object and use them to populate the properties of a .NET object. To make this process as natural as possible, Entity Framework Core supports LINQ for querying the database, which makes working with collections of objects stored in a database similar to working with collections of objects in memory.
About This Book
In this book, I explain how to use Entity Framework Core in ASP.NET Core MVC applications. I show you the different ways that Entity Framework Core can be added to a project and the pitfalls that await the unwary. Entity Framework Core is a powerful, tool but it is important to pay attention to the details; otherwise, you will end up with an application that doesnt perform well or doesnt behave as expected.
What Do You Need to Know?
To get the most from this book, you should already be familiar with ASP.NET Core MVC development. You will struggle if you dont already know how controllers and actions work and how Razor views behave. If you are not familiar with ASP.NET Core MVC, then consult https://docs.microsoft.com/en-us/aspnet/core or my book Pro ASP.NET Core MVC 2 , published by Apress.
What Software Do You Need?
To follow the examples in this book, you will need a PC running Windows, the .NET Core SDK, and the latest version of Visual Studio. With the exception of Windows, all the tools that I use in this book are available without charge, and I explain how to set up your development environment in Chapter .
What If You Dont Want to Use Windows?
I have relied on Windows and Visual Studio throughout this book because that is what most readers use, and being able to use LocalDBthe Windows-only developer version of SQL Servermakes the examples more predictable and reliable. With a little effort, you can run all of the examples in this book on any platform that .NET Core supports, although you will need to install the full SQL Server product (or use Docker containers). Contact me at adam@adam-freeman.com if you want to use Linux or macOS, and I will try to help you get started.
What Is the Structure of This Book?
This book is split into three parts, each of which covers a set of related topics.
Part 1: Introducing Entity Framework Core 2
The best way to learn is by doing, and in this part of the book, you get a high-level view of how Entity Framework Core works and how it integrates with ASP.NET Core MVC. In Chapter are given over to the development of a project called SportsStore, through which I show you a realistic development process, touching on the most important Entity Framework Core features and explaining where in the book each of them is described in detail.
Part 2: Entity Framework Core 2 in Detail
In Part 2, I describe the core features of Entity Framework Core that you will use on a day-to-day basis in your ASP.NET Core MVC projects. I show you how each feature works, explain the role it plays, and describe alternative techniques when they are available.
Part 3: Advanced Entity Framework Core 2
In Part 3, I describe the advanced features that Entity Framework Core provides. These are the features that you are unlikely to need often but that are invaluable when the default Entity Framework Core behavior doesnt solve your problem.
Where Can You Get the Example Code?
You can download the example projects for all the chapters in this book from https://github.com/apress/pro-ef-core-2-for-asp.net-core-mvc . The download is available without charge and contains all of the classes, views, and other assets required to follow the examples without having to type in all of the code.
Where Can You Get Corrections for This Book?
You can find errata for this book at https://github.com/apress/pro-ef-core-2-for-asp.net-core-mvc .
How Can You Contact Me?
If you have problems making the examples in this chapter work or if you find a problem in the book, then you can e-mail me at adam@adam-freeman.com , and I will try my best to help. Please check the errata for this book to see whether it contains a solution to your problem before contacting me.