简体   繁体   English

包含扩展方法的类库参考

[英]Class library reference for Include extension method

I am reading a tutorial http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application 我正在阅读教程http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in -asp-net-mvc应用程序

There I found some code like 在那里我找到了一些类似的代码

viewModel.Instructors = db.Instructors
    .Include(i => i.OfficeAssignment)

I like to understand the .Include method. 我想了解.Include方法。 Where is the reference? 参考在哪里? (I am expecting something like class library reference) (我期望类库参考之类的东西)

To better understand how it's working, let's take these sample Entity classes: 为了更好地了解它的工作方式,让我们看一下这些示例实体类:

 public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
    public virtual ICollection<Teacher> Teachers { get; set; }
}

public class StudentAddress 
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int Zipcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }

    public virtual ICollection<StudentAddressDetail> StudentAddressDetails { get; set; }

    public virtual Student Student { get; set; }
}

Eager Loading 渴望加载

The Eager Loading function is useful when you want to load the main Entity (or Entity collection) together with its related entities right from the start, possibly using a single query command. 当您想从一开始就可以使用单个查询命令将主要实体(或实体集合)及其相关实体一起加载时,“急切加载”功能非常有用。 In order to use that you need to use the Include() method in the following way: 为了使用它,您需要通过以下方式使用Include()方法:

using (var ctx = new SchoolDBEntities())
{
    // Loads the students AND all related StudentAddress using Eager Loading
    IList<Student> sList = ctx.Students.Include(s => s.StudentAddress).ToList<Student>();
    Student s = sList[0];
}

IMPORTANT : If you can't find the Include() method, check that you added the System.Data.Entity namespace. 重要说明如果找不到Include()方法,请检查是否添加了System.Data.Entity命名空间。

You can also use Eager Loading to load nested, multi-level properties. 您也可以使用“急切加载”来加载嵌套的多层属性。 For example, we could load the StudentAddressDetails collection property of each StudentAddress item (cfr. the StudentAddress class definition above) in the following way: 例如,我们可以通过以下方式加载每个StudentAddress项的StudentAddressDetails集合属性(参见上面的StudentAddress类定义):

using (var ctx = new SchoolDBEntities())
{
    // Loads the students AND all related StudentAddress AND all related StudentAddressDetails using Eager Loading
    IList<Student> sList = ctx.Students.Include(s => s.StudentAddress.StudentAddressDetails).ToList<Student>();
    Student s = sList[0];
}

This has been well explained here . 这已经在这里得到了很好的解释。 The answer above is an extract from this post: 上面的答案是这篇文章的摘录:

Add a using System.Data.Entity; 添加一个using System.Data.Entity; in your file. 在您的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM