Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:22:33+00:00 2026-06-02T20:22:33+00:00

Good Day! I use EF 5 Beta 2 CodeFirst. For my entities first I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:22:35+00:00Added an answer on June 2, 2026 at 8:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have a library project that i use to create a paid
Good day! I am trying to use jquery for the first time. And i
Good day! I want to create a function wherein I need to use JSON
Good day. I am just wondering how to create a base class and use
I've read that It's a good experience to use a fresh image every day
Good day everyone. I am working on a Firefox extension, and I want to
Good day all I have the following question: I would like to use Chart
I'm find such a gem a whole day, but not find a good one.
Good day to you, I use Castor to map network infrastructure into Java classes.
Good day! In his Effective STL Scott Meyers wrote A third is to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.