![]() ![]() Select extension method is used to include the Student, Grade and Teacher entities in the result. The following example demonstrates the projection query to load the Student, Grade, and Teacher entities. We can also load multiple related entities by using the projection query instead of Include() or ThenInclude() methods. The above will execute the following SQL queries in the database. The ThenInclude method must be called after the Include method. ThenInclude(g => g.Teachers) will load the Teacher collection property of the Grade entity. Include(s => s.Grade) will load the Grade reference navigation property of the Student entity. Var student = (s => s.FirstName = "Bill") ĮF Core introduced the new ThenInclude() extension method to load multiple levels of related entities. The above query will execute two SQL queries in a single database round trip. Var studentWithGrade = (s => s.FirstName = "Bill") Use the Include() method multiple times to load multiple navigation properties of the same entity.įor example, the following code loads Grade and StudentCourses related entities of Student. (1).Include() is not possible in EF Core 2.0. Note: The Include() extension method cannot be used after the DbSet.Find() method. FromSql( "Select * from Students where FirstName ='Bill'") The Include() extension method can also be used after the FromSql() method, as shown below. The example above is not recommended because it will throw a runtime exception if a property name is misspelled or does not exist.Īlways use the Include() method with a lambda expression, so that the error can be detected during compile time. We can also specify property name as a string in the Include() method, same as in EF 6. The above query executes the following SQL query in the database. Include(s => s.Grade) passes the lambda expression s => s.Grade to specify a reference property to be loaded with Student entity data from the database in a single SQL query. Unlike EF 6, we can specify a lambda expression as a parameter in the Include() method to specify a navigation property as shown below. ![]() (EF 6 does not support the ThenInclude() method.) In addition to this, it also provides the ThenInclude() extension method to load multiple levels of related entities. Įntity Framework Core supports eager loading of related entities, same as EF 6, using the Include() extension method and projection query. This will execute the following query in the database:Įxec sp_executesql N 'SELECT. In the above L2E query, we have included the GetName() C# function in the Where clause. Var studentsWithSameName = context.Students C#/VB.NET Functions in QueriesĮF Core has a new feature in LINQ-to-Entities where we can include C# or VB.NET functions in the query. Here, you will learn the new features of querying introduced in Entity Framework Core. Visit the LINQ-to-Entities chapter to learn more about the basics of querying in Entity Framework. Querying in Entity Framework Core remains the same as in EF 6.x, with more optimized SQL queries and the ability to include C#/VB.NET functions into LINQ-to-Entities queries. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |