繁体   English   中英

实体更新后,实体框架仍然为我提供了旧信息吗?

[英]after entity update entity framework still gives me old info?

我有一个奇怪的问题,我正在服务器中使用AngularJs和Web API构建单页应用程序,正在使用Entity框架,使用Code First方法一切都很好,直到我想为用户实现Change password ,更新是正确的,但是当用户尝试使用新凭据重新连接时,实体框架将收集旧密码!

public class AuthenticationFilter : ActionFilterAttribute
{
    private  MyDbRepository repo;

    public KhbyraAuthenticationFilter()
    {
        repo = new MyDbRepository(new MyDbContext());
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //Login code accessing database by repo object !!
        //Here where Entity framework gather old info
    }
 }

多数民众赞成在SecurityController中的登录操作

[EnableCors("*", "*", "*")]
public class SecurityController : BaseApiController
{

   //other actions


    [AuthenticationFilter]
    [Route("Token")]
    [HttpPost]
    public IHttpActionResult Login([FromBody]User user)
    {
        if (user == null)
        {
            Unauthorized();
        }
        return Ok();

    }
}

编辑

这是变更通过的地方

[EnableCors("*", "*", "*")]
[KhbyraAuthorizeAttribute]
public class UserController : BaseApiController
{
    private int CurrentUserID;
    public UserController():base(new KhbyraRepository(new KhbyraContext()))
    {

    }
    //.. other actions
    //..


    [Route("User/ChangePassword")]
    [HttpPost]
    public IHttpActionResult ChangePassword([FromBody]ChangePasswordModel model)
    {
       // here where i save the new password

    }

您必须在AuthenticationFilterOnActionExecuting方法内实例化新的存储库。 该过滤器是一个单例,因此您要保留一个DbContext实例,该实例已缓存旧值。

public class AuthenticationFilter : ActionFilterAttribute
{
    public KhbyraAuthenticationFilter()
    {
    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        using(var repo = new MyDbRepository(new MyDbContext()))
        {
            //Login code accessing database by repo object.
        }
    }
 }

这也使代码线程安全(当前不是)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM