Entity Framework主从表数据加载方式

 更新时间:2022年06月13日 14:38:13   作者:springsnow  
这篇文章介绍了Entity Framework主从表数据加载方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、延迟加载:LazyLoading

使用延迟加载,关联的实体必须标注为virtual。

本例是标注Destination类里的Lodgings为virtual。因为先发sql去查询主键对象,然后根据主键id去从表里查相关联的数据。

    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();

           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);
        }
    }

改进:在数据库中操作,显示加载

    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);
        }
    }

二、贪婪加载:EagerLoading

    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);
            }
        }
    }

三、显示加载:ExplicitLoading

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

    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);
        }
    }

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

    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);
        }
    }

到此这篇关于Entity Framework主从表数据加载方式的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#实现伪装文件夹功能

    C#实现伪装文件夹功能

    这篇文章主要为大家详细介绍了如何利用C#实现伪装文件夹的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • C#如何连接数据库

    C#如何连接数据库

    这篇文章主要介绍了C#如何连接数据库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • C#实现简单的计算器功能完整实例

    C#实现简单的计算器功能完整实例

    这篇文章主要介绍了C#实现简单的计算器功能,结合完整实例形式分析了C#实现常见的数学运算、进制转换等相关操作技巧与界面布局方法,需要的朋友可以参考下
    2017-08-08
  • C#校验时间格式的场景分析

    C#校验时间格式的场景分析

    本文通过场景分析给大家讲解C#里如何简单的校验时间格式,本次的场景属于比较常见的收单API,对第三方的订单进行签名验证,然后持久化到数据库,需要的朋友跟随小编一起看看吧
    2022-08-08
  • C#调用Java类的实现方法

    C#调用Java类的实现方法

    以下是对使用C#调用Java类的实现方法进行了详细的介绍,需要的朋友可以过来参考下
    2013-09-09
  • C#中使用Microsoft Unity记录日志

    C#中使用Microsoft Unity记录日志

    这篇文章介绍了C#中使用Microsoft Unity记录日志的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • C#中static void Main(string[] args) 参数示例详解

    C#中static void Main(string[] args) 参数示例详解

    这篇文章主要介绍了C#中static void Main(string[] args) 参数详解,本文通过具体示例给大家介绍的非常详细,需要的朋友可以参考下
    2017-03-03
  • C# Unity使用正则表达式去除部分富文本的代码示例

    C# Unity使用正则表达式去除部分富文本的代码示例

    正则表达式在我们日常开发中的用处不用多说了吧,下面这篇文章主要给大家介绍了关于C# Unity使用正则表达式去除部分富文本的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • C#/VB.NET实现在Word中插入或删除脚注

    C#/VB.NET实现在Word中插入或删除脚注

    脚注,是可以附在文章页面的最底端的,对某些东西加以说明,印在书页下端的注文。这篇文章将为您展示如何通过C#/VB.NET代码,以编程方式在Word中插入或删除脚注,需要的可以参考一下
    2023-03-03
  • c#连接mysql数据库的方法

    c#连接mysql数据库的方法

    这篇文章主要介绍了c#连接mysql数据库的方法,需要的朋友可以参考下
    2014-04-04

最新评论