同步加載關係實體

 Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query.

 Eager loading is achieved by use of the Include method

using (var context = new UnicornsContext())

{

    // Load all princesses and related unicorns

    var princesses1 = context.Princesses

                          .Include(p => p.Unicorns)

                          .ToList();

 

    // Load one princess and her related unicorns

    var princess1 = context.Princesses

                        .Where(p => p.Name == "Cinderella")

                        .Include(p => p.Unicorns)

                        .FirstOrDefault();

 

    // Load all princesses and related unicorns using a string

    // to specify the relationship

    var princesses2 = context.Princesses

                          .Include("Unicorns")

                          .ToList();

 

    // Load one princess and her related unicorns using a string

    // to specify the relationship

    var princess2 = context.Princesses

                        .Where(p => p.Name == "Cinderella")

                        .Include("Unicorns")

                        .FirstOrDefault();

}

Note that Include is an extension method in the System.Data.Entity namespace

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章