MembershipUser not getting updated result from database

纵然是瞬间 提交于 2019-12-12 02:40:02

问题


I'm building a simple application where a user can edit their profile including adding/deleting a brand image. This seems to be working fine and is updating the database no problem, however when refreshing the page and retrieving the user details via Membership.GetUser() the result includes the old results and not those from the updated database.

Here is my MembershipUser GetUser override:

public override MembershipUser GetUser(string query, bool userIsOnline)
    {
        if (string.IsNullOrEmpty(query))
            return null;

        var db = (AccountUser)null;

        // ...get data from db
        if (query.Contains("@")){
            db = _repository.GetByQuery(x => x.Email == query).FirstOrDefault();
        }
        else
        {
            string firstName = query;
            string lastName = null;
            if (query.Contains(" "))
            {
                string[] names = query.Split(null);
                firstName = names[0];
                lastName = names[1];
            }

            // ...get data from db
            db = _repository.GetByQuery(x => x.FirstName == firstName && x.LastName == lastName).FirstOrDefault();
        }


        if (db == null)
            return null;

        ToMembershipUser user = new ToMembershipUser(
                    "AccountUserMembershipProvider",
                    db.FirstName + " " + db.LastName,
                    db.ID,
                    db.Email,
                    "",
                    "",
                    true,
                    false,
                    TimeStamp.ConvertToDateTime(db.CreatedAt),
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue);

        // Fill additional properties
        user.ID = db.ID;
        user.Email = db.Email;
        user.FirstName = db.FirstName;
        user.LastName = db.LastName;
        user.Password = db.Password;
        user.MediaID = db.MediaID;
        user.Media = db.Media;
        user.Identity = db.Identity;
        user.CreatedAt = db.CreatedAt;
        user.UpdatedAt = db.UpdatedAt;

        return user;
    }

Note I am using a custom MembershipProvider and MembershipUser. Here is where I am calling that method:

public ActionResult Edit()
    {
        ToMembershipUser toUser = Membership.GetUser(User.Identity.Name, true) as ToMembershipUser;

Now when I do a separate query just under this line of code straight to the database, not invoking MembershipUser, I get the updated result which in turn updates the MembershipUser result?!

It seems it may be caching the results? Anyway around this? I hope this is clear enough. Thanks

Edit: It appears that when I set a breakpoint just after :

// ...get data from db
            db = _repository.GetByQuery(x => x.FirstName == firstName && x.LastName == lastName).FirstOrDefault();

'db' retrieves the outdated results though surely this is talking to the database? If need be I'll update with my repository pattern


回答1:


I managed to find a workaround though I'm not happy with this solution, so if anyone can improve upon this please post.

I decided to manually update the MembershipUser instance manually each time I update the image. My controller now looks like this:

private static ToMembershipUser MembershipUser { get; set; }

// GET: Dashboard/AccountUsers/Edit
public ActionResult Edit()
{
    if(MembershipUser == null)
        MembershipUser = Membership.GetUser(User.Identity.Name, true) as ToMembershipUser;
}


[HttpPost]
    [ValidateJsonAntiForgeryToken]
    public JsonResult UploadMedia(IEnumerable<HttpPostedFileBase> files, int id)
    {
        var images = new MediaController().Upload(files);

        if (images == null)
        {
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json("File failed to upload.");
        }

        AccountUser accountUser = db.AccountUsers.Find(id);
        db.Entry(accountUser).State = EntityState.Modified;
        accountUser.UpdatedAt = TimeStamp.Now();
        accountUser.MediaID = images[0];
        db.SaveChanges();

        MembershipUser.Media = accountUser.Media;
        MembershipUser.MediaID = accountUser.MediaID;

        return Json(new { result = images[0] });
    }

    [HttpPost]
    [ValidateJsonAntiForgeryToken]
    public JsonResult DeleteMedia(int id)
    {
        bool delete = new MediaController().Delete(id, 1);

        if (!delete)
        {
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json("Error. Could not delete file.");
        }

        MembershipUser.Media = null;
        MembershipUser.MediaID = null;

        return Json("Success");
    }


来源:https://stackoverflow.com/questions/35020582/membershipuser-not-getting-updated-result-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!