We can see that EF Core utilized the Discriminator column, even though there is no explicit mention of it in our database model. Let’s take a look at the generated SQL from our LINQ statement. EF Core comes with a ToQueryString extension method to apply to IQueryable instances to retrieve the generated SQL string. We had mentioned that both a Movie and a Series could be a Production.
EF Core 5 applies an approach known as Table-per-hierarchy (TPH), where we store hierarchically related entities in a single table. To get started, I recommend folks install the latest version of the .NET 5 SDK. Technically, EF Core 5 can run on .NET Core 3.1, but aligning versions is always a good idea. A third version of Entity Framework, version 4.1, was released on April 12, 2011, with Code First support. We assure you that you will not find any problem with the Entity Framework tutorial.
Seeding initial data
Now your object directly works with the database to retrieve or make changes. In the code-first approach, EF Core API creates the database and tables using migration based on the what is entity framework conventions and configuration provided in your domain classes. To get started using EF Core 5, we need to define a class that derives from DbContext and contains our models.
Great, now that we have built our entities and relationships, let’s add them to our DbContext instance, which we’ll call EntertainmentDbContext. We’ll also want to define our database connection string for demo purposes. With the .NET 5 release comes the newest Entity Framework Core version, unsurprisingly called Entity Framework Core 5 (EF Core 5). The .NET community got its first glimpse of Entity Framework (EF) on August 11th, 2008, with the release of .NET Framework 3.5 Service Pack 1.
EF Core mainly targets the code-first approach and provides little support for the database-first approach because the visual designer or wizard for DB model is not supported as of EF Core. EF API translates LINQ-to-Entities https://deveducation.com/ queries to SQL queries for relational databases using EDM and also converts results back to entity objects. We’ll keep the subject simple, but we’ll also explore some important EF Core concepts.
Version 5.0.0 was released on August 11, 2012 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. The following figure illustrates where the Entity Framework fits into your application.
Entity Framework Code First Approach
It saves the data in the business properties entities and also retrieves the data from the database and converts it into the business entity’s object automatically. Before .NET Framework 3.5, as a developer, we often used to write ADO.NET Code to perform CRUD operations with the underlying database. For this, we need to create a Connection Object with the database, then Open the Connection, Create the Command Object, and execute the Command using Data Reader or Data Adapter. And then, we create DataSet or DataTables to store the data in memory to perform different types of Operations on the Data as per the business requirements. It uses EDM in building SQL queries from LINQ queries, building INSERT, UPDATE, and DELETE commands, and transform database result into entity objects. Additionally, we can ask Rider to diagram our database tables, showing the relationships that we modeled during the code-first design phase of this tutorial.
- Typically, our classes will map one to one with our database tables, and relationships between tables are defined using navigational conventions.
- Additionally, we will want to install the dotnet-ef command-line tool (CLI).
- We’ll keep the subject simple, but we’ll also explore some important EF Core concepts.
Entity Framework will execute the relevant query in the database and then materialize results into instances of your domain objects for you to work within your app. That means the Entity Framework eliminates the need for writing the data-access code that developers usually need to write. As per the above figure, Entity Framework fits between the business entities (domain classes) and the database.