MembershipUser未从数据库

时间:2016-01-26 17:55:53

标签: c# asp.net asp.net-mvc asp.net-membership

我正在构建一个简单的应用程序,用户可以编辑其个人资料,包括添加/删除品牌形象。这似乎工作正常,并且更新数据库没有问题,但是当刷新页面并通过Membership.GetUser()检索用户详细信息时,结果包括旧结果,而不是更新数据库中的结果。

这是我的MembershipUser GetUser覆盖:

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;
    }

注意我使用的是自定义MembershipProvider和MembershipUser。这是我调用该方法的地方:

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

现在当我在这行代码下直接向数据库执行单独的查询,而不是调用MembershipUser时,我得到更新的结果,这反过来更新了MembershipUser结果?!

似乎可能会缓存结果?无论如何围绕这个?我希望这很清楚。感谢

编辑: 看来我刚刚设置了一个断点:

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

'db'检索过时的结果虽然这肯定是在与数据库交谈?如果需要,我将使用我的存储库模式更新

1 个答案:

答案 0 :(得分:0)

虽然我对这个解决方案不满意,但我设法找到了一个解决方法,所以如果有人可以改进,请发帖。

每次更新图片时,我都决定手动更新MembershipUser实例。我的控制器现在看起来像这样:

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");
    }