【C语言教程】Entity Framework主从表数据加载方式详解

零 C语言教程评论139字数 2315阅读7分43秒阅读模式

所需工具:

C++

聪明的大脑文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

勤劳的双手文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

注意:本站只提供教程,不提供任何成品+工具+软件链接,仅限用于学习和研究,禁止商业用途,未经允许禁止转载/分享等文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

教程如下

一、延迟加载:LazyLoading

使用延迟加载,关联的实体必须标注为virtual。文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

本例是标注Destination类里的Lodgings为virtual。因为先发sql去查询主键对象,然后根据主键id去从表里查相关联的数据。文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

[php]
private static void TestLazyLoading()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

var distanceQuery = from l in canyon.Lodgings //延迟加载canyon的所有.Lodgings
where l.Name == "HuangShan Hotel"
select l;
foreach (var lodging in distanceQuery)
Console.WriteLine(lodging.Name);
}
}
[/php]文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

改进:在 数据库中操作,显示加载文章源自灵鲨社区-https://www.0s52.com/bcjc/cyyjc/12489.html

[php]
private static void QueryLodgingDistancePro()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();
var lodgingQuery = context.Entry(canyon).Collection(d => d.Lodgings).Query();//接下来的查询在数据库中,包括Count()等
var distanceQuery = from l in lodgingQuery
where l.Name == "HuangShan Hotel"
select l;

foreach (var lodging in distanceQuery)
Console.WriteLine(lodging.Name);
}
}
[/php]

二、贪婪加载:EagerLoading

[php]
private static void TestEagerLoading()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
// var allDestinations = context.Destinations.Include(d => d.Lodgings);
var AustraliaDestination = context.Destinations.Include(d => d.Lodgings).Where(d => d.Name == "Bali");
//context.Lodgings.Include(l => l.PrimaryContact.Photo);
//context.Destinations.Include(d => d.Lodgings.Select(l => l.PrimaryContact));
//context.Lodgings.Include(l => l.PrimaryContact).Include(l => l.SecondaryContact);
foreach (var destination in AustraliaDestination)
{
foreach (var lodging in destination.Lodgings)
Console.WriteLine(" - " + lodging.Name);
}
}
}
[/php]

三、显示加载:ExplicitLoading

1、查找导航属性为一个集合的

[php]
private static void LoadRelateData()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();

context.Entry(canyon).Collection(d => d.Lodgings).Load(); //显示加载

foreach (var lodging in context.Lodgings.Local)
Console.WriteLine(lodging.Name);
}
}
[/php]

2、查找导航属性为一个实体对象的

[php]
private static void LoadPrimaryKeyData()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var lodging = context.Lodgings.First();

context.Entry(lodging).Reference(l => l.Destination).Load();

foreach (var destination in context.Destinations.Local) //遍历的是内存中的Destinations数据
Console.WriteLine(destination.Name);
}
}
[/php]

 

 

零
  • 转载请务必保留本文链接:https://www.0s52.com/bcjc/cyyjc/12489.html
    本社区资源仅供用于学习和交流,请勿用于商业用途
    未经允许不得进行转载/复制/分享

发表评论