zoukankan      html  css  js  c++  java
  • Entity Framework关联实体的三种加载方法

    推荐文章

    EF性能之关联加载 总结很好

    一:介绍三种加载方式

    Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和熊掌不能兼得。但是,通过对EF的学习,可以避免不必要的性能损失。本篇只介绍关联实体的加载的相关知识,这在我之前的文章中都有介绍。
    
    我们已经了解到EF的关联实体加载有三种方式:Lazy Loading,Eager Loading,Explicit Loading,其中Lazy Loading和Explicit Loading都是延迟加载。
    
    (一)Lazy Loading使用的是动态代理,默认情况下,如果POCO类满足以下两个条件,EF就使用Lazy Loading:
    
    POCO类是Public且不为Sealed。
    导航属性标记为Virtual。
    关闭Lazy Loading,可以将LazyLoadingEnabled设为false,如果导航属性没有标记为virtual,Lazy Loading也是不起作用的。
    
    (二)Eager Loading使用Include方法关联预先加载的实体。
    
    (三)Explicit Loading使用Entry方法,对于集合使用Collection,单个实体则使用Reference。

    2-1:Lazy Loading模式

    作用

    /*在读取父类的时候自动将所有关联类的实体都加载出来
    **比如
    */
            public class Site
            {
                [Key]
                public int Id{ get; set; }
                public string Name { get; set; }
    
                /*virtual 知识点关键字:EF三种关联加载  Lazy Loading,Eager Loading,Explicit Loading*/
                public virtual ICollection<News> Newss { get; set; }
            }
    /*通过db.Site.FirstOrDefault();会加载出一个符合条件的Site实体,并且Site实体下的所有News对象也都**加载出来并且保存在Newss中
    */

    使用方法

    两步
    
    第一:在需要延迟加载的属性前加上virtual ,该属性的类型可以是任务的集合类型ICOLLOCT<T>或者是0/1..1关联属性。
    
    如: public virtual List<Product> Products { get; set; }
    
    第二:在context构造器中开启延迟加载功能
    
    Configuration.LazyLoadingEnabled = true;    //EF6 之前的版本可能是ContextOptions.LazyLoadingEnabled = true;

    涨姿势

    //既支持在Content构造器中一次设置,程序全部通用
    //也可以在程序执行查询等命令之前动态设置
    //比如
                using (var db = new SpriderContent())
                {
                    db.Configuration.LazyLoadingEnabled = false
                    OrgPlate plate = db.OrgPlates.FirstOrDefault();
                }

    2-2:Eager Loading模式

    作用:

    EF不会再帮你把关联实体加载出来,而只会把当前表的内容读取出来,提高效率

    使用方法:

    //同Lazy Loading刚好相反  dConfiguration.LazyLoadingEnabled = false
    private static void EagerLoading(EFLoadingContext ctx)
            {
                //发送一条查询到数据库库,查询所有的province并关联city和governor
                var list = ctx.Provines.Include(t => t.Cities).Include(t => t.Governor);
                foreach (var province in list)
                {
                    //不管ctx.Configuration.LazyLoadingEnabled为false,还是没有标注导航属性virtual,都不会抛出异常
                    Print(province);
                }
            }
    
    //例子来源于http://www.cnblogs.com/nianming/p/3494781.html

    2-3:Explicit Loading模式

    使用方法

    1:Configuration.LazyLoadingEnabled = false;
    2:Explicit Loading使用Entry方法,对于集合使用Collection,单个实体则使用Reference。
        void ExplicitLoading(EFLoadingContext ctx)
        {
                //发送一条查询到数据库,查询所有的province
            var list = ctx.Provines.ToList();
            foreach (var province in list)
                {
                    var p = ctx.Entry(province);
                    //发送一条查询,查询所有当前province的city
                    p.Collection(t => t.Cities).Load();
                //发送一条查询,查询当前province的governor
                p.Reference(t => t.Governor).Load();
                //不管ctx.Configuration.LazyLoadingEnabled为false,还是没有标注导航属性virtual,都不会抛出异常
                Print(province);
            }
        }
    //例子引用自http://www.cnblogs.com/nianming/p/3494781.html
  • 相关阅读:
    Oracle查询一个用户的所有表的结构信息的SQL语句
    Unable to locate provider for protocol: smtp
    IE6 IE7 IE8(Q) 不支持 JSON 对象
    如何获取tinyeditor编辑器里面的值
    POJ 2513 Colored Sticks 字典树 + 并查集 + 欧拉路
    HDOJ 2574 Hdu Girls' Day
    HDOJ 2087 剪花布条 KMP算法
    HDOJ 2094 产生冠军
    NYOJ525 一道水题
    POJ 2406 power strings KMP中next函数的应用
  • 原文地址:https://www.cnblogs.com/farcall/p/4850458.html
Copyright © 2011-2022 走看看