Frage

Good Day! I use EF 5 Beta 2 CodeFirst. For my entities first I created ConsoleApplication and Repository working well, but when I create dll and use this Repository I have ObjectDisposedException, when tried to use Repository.
Sample (in 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
                   };
    }

Or:

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

in context.CurrentTestings.Add(newCur);

And my GetElementById (not good):

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

Thx for you help.

War es hilfreich?

Lösung

Say it politely - this code is horrible.

Just few major issues:

  1. The code never saves modified data - user = newUser; will not make your data persisted. You must use context.Entry(user).CurrentValues.SetValues(newUser) instead
  2. Do you understand purpose of generics? Why are you using switch statements by type? DbContext allows you working with sets in generic way by using: context.Set<T>()
  3. This context.Users.ToList() will load whole content of database table to your application where you search for single record. That is one of the best performance issues you can ever make. Use context.Set<T>().SingleOrDefault(x => x.Id == id)

To understand your exception you must first find where it occurs and what object is disposed. For example do you use navigation properties on your entities? Do you expect them to be loaded? In such case you must do it prior to disposing the context.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top