LINQ TO SQL :: Data Update Problem

2019-08-12 05:41发布

问题:

i am developing a project (using 3-tier approach) in which i am using LINQ TO SQL... i want to update user...

but i am facing some problem. it does not give me any error but also do not update the user detail

here is the program sequence;

in UpdateProfile.aspx

String currentUser = Session["BMUser"].ToString();

            String displayName = txtDisplayName.Text;
            String username = currentUser;
            String emailAddress = txtEmailAddress.Text;
            String secretQuestion = txtSecretQuestion.Text;
            String secretAnswer = txtSecretAnswer.Text;

                if (UserManager.UpdateProfile(username, displayName, emailAddress, secretQuestion, secretAnswer))
                {
                    lblStatus.Text = "Profile Updated";
                }
                else
                    lblStatus.Text = "Unable to Update Profile"; 

the UserManager is BLL class

public class UserManager
{           
        public static bool UpdateProfile(String username, String displayName, String emailAddress, String secretQuestion, String secretAnswer)
        {

        // This method will return BM_User (BM_User in entity class generated by LINQ TO SQL)       

            BM_User user = UserCatalog.GetUserByName(username);
            if (user != null)
            {
                user.DisplayName = displayName;
                user.EmailAddress = emailAddress;
                user.SecretQuestion = secretQuestion;
                user.SecretAnswer = secretAnswer;               

                if (UserManagerDAO.UpdateUser(user, false))
                {
                    //HttpContext.Current.Session["BMUser"] = userToUpdate;
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
}

and finally UserManagerDAO

public class UserManagerDAO
{
   public static bool UpdateUser(BM_User user, bool changeLoginDateTime)
        {
            BugManDataContext db = new BugManDataContext();            

            if (changeLoginDateTime == true)
                user.LastLoginDate = DateTime.Now;            
            db.SubmitChanges();
            return true;
        }
}

after this when i get the detail of this updated user. it shows me previous detail. mean it is not updating the user's detail with new one...

kindly solve this problem

回答1:

In your UpdateUser method you are declaring a new DataContext, so the BM_User paramter is not attached to it and that's why the SubmitChanges method does nothing.



回答2:

The problem is that the user in UserManagerDAO.UpdateUser came from a different data context. To do this, you'll have to use LINQ's serialization if you want to cross over context boundaries.

Here's a couple ways you could get around this.

  1. Add a ROWVERSION column to your table, and persisting that with your object. Once you've done that, and you attach the object back to your data context, the update should work as expected.
  2. re-select the record in your update method, and manually copy the properties from your updated object to the selected object. You could also use reflection for this if you wanted to make it work for all of your tables instead of just this one. Sample code for this is below.

Both of those options are workarounds - the best option in my opinion would be to use the linq serialization, but if that's not an option, try one of these.

// example code for #2 above...
public class UserManagerDAO
{
    public static bool UpdateUser(BM_User user, bool changeLoginDateTime)
    {
        using(BugManDataContext db = new BugManDataContext())
        {

            // lookup the current user in the database.
            var dbUser = (from u in db.Users
                          where u.Id == user.Id
                          select u).Single();

            if (changeLoginDateTime == true)
            {
                // update all the fields from your passed in user object
                dbUser.Field1 = user.Field1;
                dbUser.Field2 = user.Field2;
                dbUser.Field3 = user.Field3;
                dbUser.LastLoginDate = DateTime.Now
                dbUser.LastLoginDate = DateTime.Now;

                db.SubmitChanges();
                return true;
            }
        }
    }
}

See the answer on this question for more infomation.