zoukankan      html  css  js  c++  java
  • ef04

    1、数据层返回的都是IQueryable

    2、EF实例创建问题

    在一个项目内多处创建EF实例会有什么问题?

    如果多个模块都新建一个EF实例,则每个模块内的数据不一定能保证同步。

    解决方案:线程唯一对象,借助HttpContext(内部已经是线程唯一对象)完成

     线程中的某一层如果新建了EF对象,则接下来的层会直接沿用这个EF对象,而不是直接新建

                DemoEntities db = null;
                if (HttpContext.Current.Items["db"] == null)
                {
                    db = new DemoEntities();
                    HttpContext.Current.Items["db"] = db;
                }
                else
                {
                    // 如果有,取出并进行类型强转
                    db = HttpContext.Current.Items["db"] as DemoEntities;
                }

    3、

    IQueryable有延迟加载

    如果IQueryable想变成IEnumalbe,使用ToList方法即可、

    那怎么区分使用呢?

    IEnumable 立马放在内存中

    IQueryable 数据层返回,业务层也可以使用延迟加载机制

  • 相关阅读:
    Counting Stars hdu
    Color it hdu
    steins;Gate
    原根
    3-idiots
    Tree
    洛谷P1352 没有上司的舞会
    洛谷P1131 时态同步
    洛谷P3177 树上染色
    Codeforces Round #617 (Div. 3)
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12447610.html
Copyright © 2011-2022 走看看