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 6122939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:57:28+00:00 2026-05-23T15:57:28+00:00

I used to have a WORKING database for my ASP.NET site, up until the

  • 0

I used to have a WORKING database for my ASP.NET site, up until the point that I’ve learned that using static Database Context objects throughout the app lifetime is a very bad practice.

So, initially, I used to have this:

    public class Database
    {
        MainDBContainer db;
        static Database()
        {
            db = new MainDBContainer();
        }
    }

    internal static SiteUser GetUser(string username)
    {
         return db.SiteUserSet.SingleOrDefault(u => u.Username == username);
    }

And, as you may guess, I’ve always used this static object to access/modify the database. Well, because of what I’ve read everywhere (that the static db context is bad), I decided to change the code to this:

    internal static SiteUser GetUser(string username)
    {
        //notice no static constructor for class.

        using (MainDBContainer db = new MainDBContainer())
        {
            return db.SiteUserSet.SingleOrDefault(u => u.Username == username);
        }
    }

The problem with this code (not particularly “this” very code, but the codes that modify something. by meaning this, I meant that I wrapped every db access with the using keyword just like this one), when db.SaveChanges() is called, it doesn’t throw any exception, but nothing happens too. Within the same context, everything works, but that’s it. NOTHING GETS WRITTEN TO THE ACTUAL SQL DATABASE. Before, I had everything set up and working and as soon as SaveChanges() was called, everything was immediately flushed to the database. My user authentication is even broken (straightforward: client sends username/passwordhash, and if its right server sends a random token, and client uses token with every request): User calls login method and successfully obtains a token, but whenever any other code interacts with the User object, from another context, the token is whatever the DB has before, and SaveChanges in the login method had no effect. It’s the same for all methods, I just gave the Login one as a simple example. Am I missing something? Did I miss something obvious while porting code? Probably yes, but what’s it?

Thanks,
Can.

  • 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-05-23T15:57:28+00:00Added an answer on May 23, 2026 at 3:57 pm

    You’ve moved to a disconnected model. When a context is destroyed, EF no longer knows which objects are associated when you create a new instance on the next CRUD operation. Essentially, you manually have to tell EF about the objects you want to persist, and how to persist them.

    For saving, you need to call AddObject on the context.

    For updating, you call AttachTo and then ChangeObjectState or SetModifiedProperty on ObjectStateManager. (Note: this is only for completely disconnected objects: if you load an object and then make changes — something you might do if you’re doing row-level versioning — EF already knows what to do because it has a built-in change-tracking mechanism, but only while the object is connected.)

    For delete, you call AttachTo and then DeleteObject.

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

Sidebar

Related Questions

I have used JQuery within my asp.net page. JQuery is working fine. I could
My Scenario, Im using asp.net 2.0 .I have Site where it creates a unique
Working inside the context of an ASP.NET application I am creating a page that
What profilers have you used when working with .net programs, and which would you
I am currently converting a very old, but working classic ASP site to ASP.Net.
Let say i have one asp.net application that is having some page that uses
I am using the built-in forms authentication that comes with asp.net mvc. I added
Here's my situation: I have been working on an ASP.NET MVC 3 application for
We've got an ASP.NET website that uses a database that we want to be
I have an asp.net mvc application, which uses the default user database. It all

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.