0

再会!我使用 EF 5 Beta 2 CodeFirst。对于我的实体,我首先创建了运行良好的 ConsoleApplication 和 Repository,但是当我创建 dll 并使用此 Repository 时,我在尝试使用 Repository 时遇到了 ObjectDisposedException。
样品(中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();
        }
    }

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

谢谢你的帮助。

4

1 回答 1

3

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

只是几个主要问题:

  1. 该代码从不保存修改过的数据——user = newUser;不会使您的数据持久化。您必须context.Entry(user).CurrentValues.SetValues(newUser)改用
  2. 你了解泛型的目的吗?为什么要按类型使用 switch 语句?DbContext 允许您通过使用以下方法以通用方式处理集合:context.Set<T>()
  3. context.Users.ToList()会将数据库表的全部内容加载到您搜索单个记录的应用程序中。这是你能做到的最好的性能问题之一。采用context.Set<T>().SingleOrDefault(x => x.Id == id)

要了解您的异常,您必须首先找到它发生的位置以及处理了什么对象。例如,您是否在实体上使用导航属性?您希望它们被加载吗?在这种情况下,您必须在处理上下文之前执行此操作。

于 2012-04-26T13:55:31.283 回答