Entity Framework Core系列教程-6-查询数据
Entity Framework Core中的查询
Entity Framework Core中的查询与EF 6.x中的查询相同,但具有更优化的SQL查询,并且能够将C#/ VB.NET函数包含在LINQ到实体查询中。
Entity Framework Core 允许你在模型中使用导航属性来加载相关实体。 有三种常见的 O/RM 模式可用于加载关联数据:
- Eager loading (预先加载): 表示从数据库中加载关联数据,作为初始查询的一部分。
- Explicit loading(显式加载): 表示稍后从数据库中显式加载关联数据。
- Lazy loading(延迟加载): 表示在访问导航属性时,从数据库中以透明方式加载关联数据。
访问LINQ-to-Entities一章,以了解有关Entity Framework中查询基础的更多信息。
在这里,您将学习Entity Framework Core中引入的查询新功能。
查询中的C#/ VB.NET函数
EF Core在LINQ-to-Entities中具有新功能,我们可以在查询中包含C#或VB.NET函数。这在EF 6中是不可能的。
private static void Main(string[] args) { var context = new SchoolContext(); var studentsWithSameName = context.Students .Where(s => s.FirstName == GetName()) .ToList(); } public static string GetName() { return "Bill"; }
在上面的L2E查询中,我们在Where子句中包含了GetName() C#函数。这将在数据库中执行以下查询:
exec sp_executesql N'SELECT [s].[StudentId], [s].[DoB], [s].[FirstName], [s].[GradeId], [s].[LastName], [s].[MiddleName] FROM [Students] AS [s] WHERE [s].[FirstName] = @__GetName_0',N'@__GetName_0 nvarchar(4000)', @__GetName_0=N'Bill' Go
Eager Loading 预先加载
Entity Framework Core支持使用Include()扩展方法和投影查询来快速加载与EF 6相同的相关实体。除此之外,它还提供了ThenInclude()扩展方法来加载多个级别的相关实体。 (EF 6不支持ThenInclude()方法。)
Include:
与EF 6不同,我们可以在Include()方法中将lambda表达式指定为参数,以指定导航属性,如下所示。
var context = new SchoolContext(); var studentWithGrade = context.Students .Where(s => s.FirstName == "Bill") .Include(s => s.Grade) .FirstOrDefault();
在上面的示例中,.Include(s => s.Grade)传递lambda表达式s => s.Grade,以指定引用属性,该属性将在单个SQL查询中与来自数据库的Student实体数据一起加载。上面的查询在数据库中执行以下SQL查询。
SELECT TOP(1) [s].[StudentId], [s].[DoB], [s].[FirstName], [s].[GradeId],[s].[LastName], [s].[MiddleName], [s.Grade].[GradeId], [s.Grade].[GradeName], [s.Grade].[Section] FROM [Students] AS [s] LEFT JOIN [Grades] AS [s.Grade] ON [s].[GradeId] = [s.Grade].[GradeId] WHERE [s].[FirstName] = N'Bill'
我们还可以在Include()方法中将属性名称指定为字符串,与EF 6中相同。
var context = new SchoolContext(); var studentWithGrade = context.Students .Where(s => s.FirstName == "Bill") .Include("Grade") .FirstOrDefault();
不建议使用上面的示例,因为如果属性名称拼写错误或不存在,则会抛出运行时异常。始终对lambda表达式使用Include()方法,以便可以在编译时检测到错误。
Include()扩展方法也可以在FromSql()方法之后使用,如下所示。
var context = new SchoolContext(); var studentWithGrade = context.Students .FromSql("Select * from Students where FirstName ='Bill'") .Include(s => s.Grade) .FirstOrDefault();
注意:DbSet.Find()方法之后不能使用Include()扩展方法。例如。在EF Core 2.0中无法使用context.Students.Find(1).Include()。在将来的版本中这可能是可能的。
多次使用Include()方法来加载同一实体的多个导航属性。例如,以下代码加载与Student的Grade和StudentCourses相关的实体。
var context = new SchoolContext(); var studentWithGrade = context.Students.Where(s => s.FirstName == "Bill") .Include(s => s.Grade) .Include(s => s.StudentCourses) .FirstOrDefault();
上面的查询将在单个数据库往返中执行两个SQL查询。
SELECT TOP(1) [s].[StudentId], [s].[DoB], [s].[FirstName], [s].[GradeId], [s].[LastName], [s].[MiddleName], [s.Grade].[GradeId], [s.Grade].[GradeName], [s.Grade].[Section] FROM [Students] AS [s] LEFT JOIN [Grades] AS [s.Grade] ON [s].[GradeId] = [s.Grade].[GradeId] WHERE [s].[FirstName] = N'Bill' ORDER BY [s].[StudentId] Go SELECT [s.StudentCourses].[StudentId], [s.StudentCourses].[CourseId] FROM [StudentCourses] AS [s.StudentCourses] INNER JOIN ( SELECT DISTINCT [t].* FROM ( SELECT TOP(1) [s0].[StudentId] FROM [Students] AS [s0] LEFT JOIN [Grades] AS [s.Grade0] ON [s0].[GradeId] = [s.Grade0].[GradeId] WHERE [s0].[FirstName] = N'Bill' ORDER BY [s0].[StudentId] ) AS [t] ) AS [t0] ON [s.StudentCourses].[StudentId] = [t0].[StudentId] ORDER BY [t0].[StudentId] Go
ThenInclude
EF Core引入了新的ThenInclude()扩展方法,以加载多个级别的相关实体。考虑以下示例:
var context = new SchoolContext(); var student = context.Students.Where(s => s.FirstName == "Bill") .Include(s => s.Grade) .ThenInclude(g => g.Teachers) .FirstOrDefault();
在上面的示例中,.Include(s => s.Grade)将加载Student实体的Grade导航属性。 .ThenInclude(g => g.Teachers)将加载Grade实体的Teacher集合属性。必须在Include方法之后调用ThenInclude方法。上面的代码将在数据库中执行以下SQL查询。
SELECT TOP(1) [s].[StudentId], [s].[DoB], [s].[FirstName], [s].[GradeId], [s].[LastName], [s].[MiddleName], [s.Grade].[GradeId], [s.Grade].[GradeName], [s.Grade].[Section] FROM [Students] AS [s] LEFT JOIN [Grades] AS [s.Grade] ON [s].[GradeId] = [s.Grade].[GradeId] WHERE [s].[FirstName] = N'Bill' ORDER BY [s.Grade].[GradeId] Go SELECT [s.Grade.Teachers].[TeacherId], [s.Grade.Teachers].[GradeId], [s.Grade.Teachers].[Name] FROM [Teachers] AS [s.Grade.Teachers] INNER JOIN ( SELECT DISTINCT [t].* FROM ( SELECT TOP(1) [s.Grade0].[GradeId] FROM [Students] AS [s0] LEFT JOIN [Grades] AS [s.Grade0] ON [s0].[GradeId] = [s.Grade0].[GradeId] WHERE [s0].[FirstName] = N'Bill' ORDER BY [s.Grade0].[GradeId] ) AS [t] ) AS [t0] ON [s.Grade.Teachers].[GradeId] = [t0].[GradeId] ORDER BY [t0].[GradeId] go
Projection Query(投影查询)
我们还可以通过使用投影查询而不是Include()或ThenInclude()方法来加载多个相关实体。以下示例演示了用于加载“学生”,“年级”和“教师”实体的投影查询。
var context = new SchoolContext(); var stud = context.Students.Where(s => s.FirstName == "Bill") .Select(s => new { Student = s, Grade = s.Grade, GradeTeachers = s.Grade.Teachers }) .FirstOrDefault();
在上面的示例中,.Select扩展方法用于在结果中包括Student, Grade and Teacher实体。这将执行与上述ThenInclude()方法相同的SQL查询。
关于延迟加载(Lazy Loading)和显式加载( Explicit Loading). EF Core 3.1 已经支持,原文为2.0版本已过时,可以参考官方文档:
参考: https://docs.microsoft.com/en-us/ef/core/querying/related-data