Entity framework有3种加载数据的方式:懒汉式(Lazy loading),饿汉式(Eager loading),显示加载(Explicit loading)。3种加载方式有各自的优缺点,详细的可以参考微软的技术文章《Reading Related Data with the Entity Framework in an ASP.NET MVC Application》
本文只针对需要一次性加载多个相关联实体的情况进行讨论。
1. 数据库模型
2. Model和DbContext
public class Student
{
public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime? EnrollmentDate { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Student.cs
public class Enrollment
{
public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public int? Grade { get; set; } public virtual Student Student { get; set; } public virtual Course Course { get; set; }
}
Enrollment.cs
public class Course
{
public int CourseID { get; set; } public string Title { get; set; } public string Credits { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Course
public class Context:DbContext
{
public Context() : base("ReadMultipleEntity")
{ } public DbSet<Student> Students { get; set; } public DbSet<Course> Courses { get; set; } public DbSet<Enrollment> Enrollments { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
} }
Context.cs
3. 需求
想要得到所有学生及其选课名称的列表
因为是想要所有学生及课程的列表信息,所以此时选择饿汉式效率最佳(一次性将所有数据加载到内存)。
context.Students.Include(x => x.Enrollments).ToList();//加载Students和Enrollments实体
通过Sql Profiler监控到所执行的SQL
SELECT
[Project1].[ID] AS [ID],
[Project1].[LastName] AS [LastName],
[Project1].[FirstMidName] AS [FirstMidName],
[Project1].[EnrollmentDate] AS [EnrollmentDate],
[Project1].[C1] AS [C1],
[Project1].[EnrollmentID] AS [EnrollmentID],
[Project1].[CourseID] AS [CourseID],
[Project1].[StudentID] AS [StudentID],
[Project1].[Grade] AS [Grade]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[LastName] AS [LastName],
[Extent1].[FirstMidName] AS [FirstMidName],
[Extent1].[EnrollmentDate] AS [EnrollmentDate],
[Extent2].[EnrollmentID] AS [EnrollmentID],
[Extent2].[CourseID] AS [CourseID],
[Extent2].[StudentID] AS [StudentID],
[Extent2].[Grade] AS [Grade],
CASE WHEN ([Extent2].[EnrollmentID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[Student] AS [Extent1]
LEFT OUTER JOIN [dbo].[Enrollment] AS [Extent2] ON [Extent1].[ID] = [Extent2].[StudentID]
) AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC
Only Enrollment
如果想将和Enrollment相关联的Course实体同时加载,则需要如下语句:
context.Students.Include(x => x.Enrollments.Select(y => y.Course)).ToList();
通过Sql Profiler监控到所执行的SQL
SELECT
[Project1].[ID] AS [ID],
[Project1].[LastName] AS [LastName],
[Project1].[FirstMidName] AS [FirstMidName],
[Project1].[EnrollmentDate] AS [EnrollmentDate],
[Project1].[C1] AS [C1],
[Project1].[EnrollmentID] AS [EnrollmentID],
[Project1].[CourseID] AS [CourseID],
[Project1].[StudentID] AS [StudentID],
[Project1].[Grade] AS [Grade],
[Project1].[CourseID1] AS [CourseID1],
[Project1].[Title] AS [Title],
[Project1].[Credits] AS [Credits]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[LastName] AS [LastName],
[Extent1].[FirstMidName] AS [FirstMidName],
[Extent1].[EnrollmentDate] AS [EnrollmentDate],
[Join1].[EnrollmentID] AS [EnrollmentID],
[Join1].[CourseID1] AS [CourseID],
[Join1].[StudentID] AS [StudentID],
[Join1].[Grade] AS [Grade],
[Join1].[CourseID2] AS [CourseID1],
[Join1].[Title] AS [Title],
[Join1].[Credits] AS [Credits],
CASE WHEN ([Join1].[EnrollmentID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[Student] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[EnrollmentID] AS [EnrollmentID], [Extent2].[CourseID] AS [CourseID1], [Extent2].[StudentID] AS [StudentID], [Extent2].[Grade] AS [Grade], [Extent3].[CourseID] AS [CourseID2], [Extent3].[Title] AS [Title], [Extent3].[Credits] AS [Credits]
FROM [dbo].[Enrollment] AS [Extent2]
INNER JOIN [dbo].[Course] AS [Extent3] ON [Extent2].[CourseID] = [Extent3].[CourseID] ) AS [Join1] ON [Extent1].[ID] = [Join1].[StudentID]
) AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC
Enrollment and Course
比较两次SQL可得,后者多join了一个Course表,从而一次性得到所有的结果。如果想在Course再连接其他实体,可以仿照上面的形式。