实体框架ObjectDisposedException

时间:2012-04-26 13:41:35

标签: .net entity-framework repository objectdisposedexception

美好的一天! 我使用EF 5 Beta 2 CodeFirst。 对于我的实体,我首先创建了ConsoleApplication和Repository,但是当我创建dll并使用此Repository时,我有ObjectDisposedException,当试图使用Repository时。
样本(在var user = (new Repository().GetElementById<ReposirotyEF.User>(resultTesting.Users.Id) as ReposirotyEF.User);中):

public static ResultModel PopulateResultModel(ResultTesting resultTesting)
{
        string name = string.Empty;

        var user = (new Repository().GetElementById<ReposirotyEF.User>(resultTesting.Users.Id) as ReposirotyEF.User);
        name = user.Name;

        return new ResultModel()
                   {
                       Id = resultTesting.Id,
                       Name = name,
                       Result = resultTesting.Result,
                       Answers = resultTesting.Answers,
                       Data = resultTesting.TimeEnd,
                       RightAnswers = resultTesting.RightAnswers,
                       Time = resultTesting.Time
                   };
    }

或者:

public void SaveOrUpdate<T>(T obj)
    {
        using (var context = new ContextTest1())
        {
            switch (typeof(T).Name)
            {
                case "User":
                    User user = context.Users.ToList().Find(u => u.Id == (obj as User).Id);
                    User newUser = obj as User;
                    if (user != null)
                    {
                        user = newUser;
                    }
                    else
                    {
                        context.Users.Add(newUser);
                    }

                    break;
                case "ResultTesting":
                    var resultTesting = context.ResultTestings.ToList().Find(u => u.Id == (obj as ResultTesting).Id);
                    var newRes = obj as ResultTesting;
                    if (resultTesting != null)
                    {
                        resultTesting = newRes;
                    }
                    else
                    {
                        context.ResultTestings.Add(newRes);
                    }

                    break;
                case "Question":
                    var question = context.Questions.ToList().Find(u => u.Id == (obj as Question).Id);
                    var newQue = obj as Question;
                    if (question != null)
                    {
                        question = newQue;
                    }
                    else
                    {
                        context.Questions.Add(newQue);
                    }

                    break;
                default:
                    //context.CurrentTestings.ToList().Remove(obj as CurrentTesting);
                    var currentTesting = context.CurrentTestings.ToList().Find(u => u.Id == (obj as CurrentTesting).Id);
                    var newCur = obj as CurrentTesting;
                    if (currentTesting != null)
                    {
                        currentTesting = newCur;
                    }
                    else
                    {
                        context.CurrentTestings.Add(newCur);
                    }

                    break;
            }
            context.SaveChanges();
        }
    }
{p} context.CurrentTestings.Add(newCur);中的

我的GetElementById(不好):

public IId GetElementById<T>(int id)
        where T : IId
    {
        using (var context = new ContextTest1())
        {
            switch (typeof(T).Name)
            {
                case "User":
                    return context.Users.ToList().Find(u => u.Id == id);
                case "ResultTesting":
                    return context.ResultTestings.ToList().Find(u => u.Id == id);
                case "Question":
                    return context.Questions.ToList().Find(u => u.Id == id);
            }

            return context.CurrentTestings.ToList().Find(u => u.Id == id);
        }
    }

谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

礼貌地说 - 这段代码太可怕了。

只有几个主要问题:

  1. 代码永远不会保存已修改的数据 - user = newUser;不会使您的数据保持不变。您必须使用context.Entry(user).CurrentValues.SetValues(newUser)代替
  2. 你了解仿制药的用途吗?为什么要按类型使用switch语句? DbContext允许您使用:context.Set<T>()
  3. 以通用方式处理集合
  4. context.Users.ToList()会将数据库表的全部内容加载到您搜索单个记录的应用程序中。这是您可以做出的最佳性能问题之一。使用context.Set<T>().SingleOrDefault(x => x.Id == id)
  5. 要了解您的异常,您必须首先找到它发生的位置以及处置的对象。例如,您是否在实体上使用导航属性?你期望它们被加载吗?在这种情况下,您必须在处置上下文之前执行此操作。