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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:18:47+00:00 2026-05-26T06:18:47+00:00

Based on my last question , I have tried to separate the business logic

  • 0

Based on my last question, I have tried to separate the business logic from my controller completely.

This however has left a problem which I understand why, but not how to fix…. And, I do not understand why it is doing what is doing.

In my controller, I had the following:

public User GetCurrentUser()
{    
    User user = db.Users.SingleOrDefault(x => x.UserName == User.Identity.Name);
    return user;
}

I now know about [NonAction] which fixes the security concern – however, I know this doesn’t follow best practices of not having any non controller stuff in a controller. For this reason, I moved to a new class and modified it to the following:

public User GetCurrentUser(string name)
{    
    User user = db.Users.SingleOrDefault(x => x.UserName == name);
    return user;
}

I have an edit method which before simply set various fields in the user object, then called db.SaveChanges(). This however is now causing issues – I believe it is due to calling the command on a db object that doesn’t actually have the object loaded.

But, the part that I really do not understand is when I am redirected back to the home page and perform GetCurrentUser() again, I am presented with the edited details I changed… These are not stored in the database and it is only when I restart the application it goes back to the database results.

I am finding this very confusing! What is happening here and where are the object being stored?

And, how do I fix it? I have tried making the new Class’s db function public and calling it’s SaveChanges() method, but, this is resulting in the same problem – data that is not being saved to the database.

Anyway, quite frankly, I really liked calling it via just GetCurrentUser(), I wanted this due to the fact I wanted to change the way the user was loaded in the future – but, now that I have to call it via GetCurrentUser(User.Identity.Name), and make other modifications, I think it wouldn’t be that much harder to just skip on the method and call the Lambda query directly instead… It just seems it will save a lot of trouble.

  • 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-26T06:18:47+00:00Added an answer on May 26, 2026 at 6:18 am

    Based on the detail in your question, you need to make sure you attaching your Entity object e.g db.Users.Attach(updatedUser)
    And then change its state
    e.g db.ObjectStateManager.ChangeObjectState(updatedUser, EntityState.Modified)

    Before you call db.SaveChanges()

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

Sidebar

Related Questions

This is based on my last question. I have these arrays: var array1 =
This is related to my last question . I have an application that I
I know this is a probable open ended question, and I have tried looking
I have already looked at and tried the resolutions to this question that others
I am trying to get the last entry from my database based off the
Based on an answer from a candidate I have a confusion regarding the functioning
Using the following code, and inspired by this question, I managed to have vertical
This has been really killing me for the last couple of days now. I
Following on from my last question on optimization I'm finding it easier to believe
This is a question based on C code for Win32. In one of my

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.