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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:39:02+00:00 2026-05-19T04:39:02+00:00

I have a problem in my UserRepository in which I want to update a

  • 0

I have a problem in my UserRepository in which I want to update a user. I dont want certain fields updated, such as password, unless specified. For example, When I pass the User from the view, to the service to the repository, it sends up the user with a null or empty password string. This null gets written to the database (which I dont want).

How do I handle a situation like this?

Domain

public class User
{
    public int UserId { get; set; }

    public string Email { get; set; }
    public string Password { get; set; }
}

Repository

    public User Save(User user)
    {
        if (user.UserId > 0)
        {
            User dbUser = context.Users.FirstOrDefault(u => u.UserId == user.UserId);
            //What do I do here?
        }
        context.Users.AddObject(user);
        context.SaveChanges();
        return user;
    }

Lets say in this case, my view allows me to change only Email, so the only thing that gets sent back to the Save() method are: user.UserId and user.Email while user.Password is null. In my case, the database throws error because Password should be nullable.

  • 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-19T04:39:02+00:00Added an answer on May 19, 2026 at 4:39 am

    Detached POCO scenario (you will not load user from DB before update):

    You can selectively say which properties must be updated:

    public User Save(User user)     
    {         
        if (user.UserId == 0)         
        {             
            context.Users.AddObject(user);         
        }
        else
        {
            context.Users.Attach(user);
            ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(user);
            entry.SetModifiedProperty("Email");
        }
    
        context.SaveChanges();         
        return user;     
    }
    

    You can also create two overloads of you Save method. First will update whole object, second will update only explicitly selected properties:

    public User Save(User user)     
    {         
        if (user.UserId == 0)         
        {             
            context.Users.AddObject(user);         
        }
        else
        {
            context.Users.Attach(user);
            context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);        
        }
    
        context.SaveChanges();         
        return user;     
    }
    
    public User Save(User user, IEnumerable<Expression<Func<User, object>>> properties)     
    {         
        if (user.UserId == 0)         
        {             
            context.Users.AddObject(user);         
        }
        else
        {
            context.Users.Attach(user);
            ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(user);
            foreach(var selector in properties)
            {
                string propertyName = PropertyToString(selector.Body);
                entry.SetModifiedProperty(propertyName);
            }
        }
    
        context.SaveChanges();         
        return user;     
    }
    
    // Doesn't work for navigation properties!
    private static string PropertyToString(Expression selector)
    {
        if (selector.NodeType == ExpressionType.MemberAccess)
        {
            return ((selector as MemberExpression).Member as PropertyInfo).Name;
        }
    
        throw new InvalidOperationException();
    }
    

    You will call the second overload this way:

    userRepository.Save(user, new List<Expression<Func<User, object>>> 
        { 
            u => u.Email 
        });
    

    Attached scenario (you will load user from DB before update):

    You can modify your Save method to accept delegate so that you can control how update will be performed:

    public User Save(User user, Action<User, User> updateStrategy)                                
    {                                  
        if (user.UserId > 0)                                  
        {
            User dbUser = context.Users.FirstOrDefault(u => u.UserId == user.UserId);
            updateStrategy(dbUser, user);                                                                        
        }        
        else
        {                          
            // New object - all properties should be saved
            context.Users.AddObject(user);
        }
    
        context.SaveChanges();                                  
        return user;                              
    }  
    

    You will call the method this way:

    var user = GetUpdatedUserFromSomewhere();
    repository.Save(user, (dbUser, mergedUser) => 
        {
            dbUser.Email = mergedUser.Email;
        });
    

    Anyway, despite of my examples you should definitely think about Darin’s post and special ModelViews for updating.

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

Sidebar

Related Questions

I have the following problem: public Boolean Exists(String userName) { IRepository<User> = new UserRepository();
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have repositories (e.g. ContactRepository, UserRepository and so forth) which encapsulate data access to
In our database we have a user. A user can have certain metadata (Like
I have a problem with derived finder methods of GraphRepository. Short version: User foundUser1
Have problem with linq expressions. I want to get from db some data ordered
I have problem with my Java program. How I read xml -file which has
I have problem on passing jquery variable on codeigniter controller. actually i want to
I have problem with binding to static property. I want to have Label with
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar

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.