EF API does not create any column for the collection navigation property in the related table of an entity,
but it creates a column in the table of an entity of generic collection. For example, the following Grade entity contains a generic collection navigation property ICollection. Here, the Student entity is specified as generic type, so EF API will create a column Grade_GradeId in the Students table in the database. This late-bound or « weakly-typed » approach to data access is prone to error. Consequently, professional developers prefer to work with data in a strongly-typed manner. In the code-first approach, EF Core API creates the database and tables using migration based on the conventions and configuration provided in your domain classes.
In EF Core, we can override the OnModelCreating method on our EntertainmentDbContext. Looking over our models, we can see some conventional patterns emerge. Naming is an essential part of EF Core modeling, where we define relationships by the Type of property definitions and their names. 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.
The SQL statement is still very readable, even when we consider the answer we are returning from our database. As you can imagine, the SQL generated by EF Core will reflect the complexity of our request. Unlike the previous query, this example only uses the Rating entity, which results in the following SQL. The first approach is to use the DbSet on our EntertainmentDbContext class. The discriminator is an important mechanism that we’ll see used later in this post’s query section. Various profilers are commercially available to troubleshoot performance issues using what is entity framework, both for EF and EF Core variants.
Entity Framework is an Object-Relational Mapper which enable the .NET developers to work with the database using the .NET objects. It eliminates the need for accessing the code of the data that developers need to write. EF API will create a column in the database table for each scalar property, as shown below. In this post, we modeled an entertainment problem domain using code-first design. In addition to modeling, we applied our schema to an SQLite database.
This section will explore common queries we will write when dealing with our domain. Executing the database update command will apply our migrations up to the latest. We have a many to many relationship between our Production and the Actors. We also have a one to many relationship, where a Production can have many instances of a Rating.
EF Core still falters around some strange edge cases, but the EF team is actively working to support more complex LINQ to SQL translations. If you’re considering working with a database engine, I highly recommend looking at EF Core as an option to jumpstart your development. Likely the most complex query in this list, let’s find actors playing characters in different productions. We’ll need to start at the Actors entity and navigate through the Characters collection while including the Production information. An element to note in our LINQ query is the value of 1 passed to the Take method. This value exists in C# but does not exist in our SQL query unless we give it to our database engine.