Note that there are some explanatory texts on larger screens.

plurals
  1. POSaveChanges() doesn't work before an exception throw
    primarykey
    data
    text
    <p>Sorry for my english, I'll do it as well as I can.</p> <p>I have the Entity "Usuario" which receives from the rbacManager the object, calling Login method (see below). This method, as you can see, throws an exception when it can't match both the user an password. In this case, I have to increment the property Intentos, to allow users to have only three opportunities to fail.</p> <p>The problem is the following: SaveChanges method is not saving the values modified in Usuario. I think that SaveChanges method is failing due to the exception, but i'm not sure. I couldn't find any help on the web.</p> <p>This is the action Method LogOn: </p> <pre><code>using(EntitiesModel dbContext = new EntitiesModel()) { try { string hPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(model.Password, "SHA1"); Usuario usuario = this.rbacManager.Login(model.UserName, hPassword); FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/") &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else return RedirectToAction("Index", "Home"); } catch (LoginIncorrectoException) { dbContext.SaveChanges(); ViewData["Message"] = "User incorrect."; } catch (UsuarioBloqueadoException) { ViewData["Message"] = "User locked"; } return View(model); } </code></pre> <p>And the method Login of the fourth line, that throws the exception:</p> <pre><code>using (EntitiesModel context = new EntitiesModel()) { Usuario usuario = this.GetUsuario(username); if (usuario == null) throw new LoginIncorrectoException(); if (!usuario.EstaActivo()) { throw new UsuarioBloqueadoException(); } if (usuario.Password != password) { usuario.Intentos++; if (usuario.Intentos &gt;= 3) { usuario.Activo = false; context.SaveChanges(); throw new UsuarioBloqueadoException(); } else { context.SaveChanges(); throw new LoginIncorrectoException(); } } usuario.Intentos = 0; return usuario; } </code></pre> <p><strong>EDIT:</strong> I copy GetUsuario method.</p> <pre><code> private Usuario GetUsuario(string username) { using (EntitiesModel context = new EntitiesModel()) { List&lt;Usuario&gt; Usuarios = context .Usuarios .Where(x =&gt; x.Username == username) .ToList(); if (Usuarios.Count == 1) return Usuarios.First(); else throw null; } } </code></pre> <p>Anyone knows why SaveChanges is not working? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload