实体框架 - 加载参考按键处理实体对象的上下文后 [英] Entity Framework - Load Reference Keys after disposing entity object context

查看:95
本文介绍了实体框架 - 加载参考按键处理实体对象的上下文后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.Net /的WebForms /实体模型/框架3.5

I am using ASP.Net / WebForms / Entity Model / Framework 3.5

下面是我的项目的结构简单
表格> BLL> DAL(使用实体模型)

Here is my project's simple structure Forms > BLL > DAL ( uses entity model )

下面是我的DAL的段

public class MyDAL : IDisposable
{
    private MyEntities db;
    public BaseDAL()
    {
        db = new MyEntities();
    }

    public User GetUserByID(int userId)
    {
        try
        {
            IQueryable<User> objUser = null;
            objUser  = from res in db.Users
                          where res.UserId == userId
                          select res;

            return objUser.FirstOrDefault();
        }
        catch
        {
            throw;
        }
    }

    public void Dispose()
    {
        db.Dispose();
    }
}

我叫DAL的功能从我BLL这样

I call the DAL's function from my BLL like this

public class MyBLL
{
    public User GetUserByID(int userId)
    {
        try
        {
            using (MyDAL objMyDAL = new MyDAL())
            {
                return objMyDAL.GetUserByID(userId);
            }
        }
        catch
        {
            throw;
        }
    }
}

我打电话到使用块的DAL所以MyDAL的处置事件很快就会火BLL返回用户对象之后。所以在这一点上的ObjectContext实例得到处理。

I am calling the DAL through using block so MyDAL's Dispose event will fire soon after the BLL returns the User object. So at this point ObjectContext instance gets disposed.

现在在我的Web窗体,我调用这个函数像这样获取用户信息和组细节是USER_GROUP表的用户表的外键

Now in my Web Form, I am calling this function like this to get user information and Group details which is a foreign key of user_Group Table in User table

    protected void Page_Load(object sender, EventArgs e)
    {
        MyBLL objMyBll = new MyBLL();

        User objUser = objMyBll.GetUserByID(123);
        objUser.User_GroupReference.Load(); // ERROR LINE
        int groupId = objUser.User_Group.Group_Id;
    }

在题咏上线时 objUser.User_Grou preference.Load(); 我得到这个例外

When the ode comes on line objUser.User_GroupReference.Load(); I get this exception

的ObjectContext的实例已设置,并且可以不再使用
  对于需要连接操作。

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

如何解决此问题?如果我不这样做 db.Dispose(); 在我的DAL的Dispose方法,它工作正常,无异常来。但是,如果我不处置db对象那里,当&安培;我应该在哪里处理呢?
和处置对象上下文后如何访问参考钥匙?

How to resolve this? If I do not do db.Dispose(); in my DAL's dispose method, it works fine and no exceptions comes. But if I do not dispose the db object there, when & where should I dispose it? And how to access Reference Keys after disposed object context?

推荐答案

因为当您访问导航属性延迟加载被触发,但延迟加载仅在用来加载实体上下文的范围,该例外被解雇了。如果您配置的背景下,你将失去延迟加载的能力。有没有办法使用环境处置后懒加载(除了实体附加到新的环境,但如果你从原来的语境中分离它在处置之前它只会工作)。

The exception is fired because lazy loading is fired when you access that navigation property but lazy loading works only within scope of context used to load the entity. If you dispose the context you will lose lazy loading ability. There is no way to use lazy loading after context disposal (except attaching the entity to the new context but it will only work if you detach it from the original context before you dispose it).

在你的体系结构必须使用包含来显式地加载每一种关系,你需要在上层。如果您想使用延迟加载上下文必须活请求的整个期间。在Web表单情况下,它可以在例如的BeginRequest EndRequest ,你在<创建环境事件处理$处理C $ C>的BeginRequest 和 EndRequest 。上下文将被存储在 HttpContext.Items 。你应该从这个集合的上下文(你可以让helper方法为),并把它传递给BLL的构造,这将反过来它传递给DAL。不要从BLL或DAL访问 HttpContext.Items

In your architecture you must use Include to explicitly load every relation you will need in upper layers. If you want to use lazy loading your context must live for the whole duration of the request. In case of web forms it can be handled for example in BeginRequest and EndRequest event handlers where you create context in BeginRequest and dispose it in EndRequest. The context will be stored in HttpContext.Items. You should get the context from this collection (you can make helper method for that) and pass it to constructor of BLL which will in turn pass it to DAL. Don't access HttpContext.Items from BLL or DAL.

这篇关于实体框架 - 加载参考按键处理实体对象的上下文后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆