例外和Linq到实体

时间:2009-12-04 20:31:15

标签: linq-to-entities exception

我有一个难以回答的问题,所以请跟我吵架。

在下面的代码中,这是有效的,因为程序员只对将物理数据层引起的特定错误投影到事件日志感兴趣。其余的被推到堆栈的更远处。这是因为他能够捕获OdbcException(s)。

我正在实施自己的MembershipProvider。但是,我使用Linq to Entities,但我想只向事件日志发送低级/物理级异常。当使用Linq to Entities时,我可以捕获一个例外,这将允许我捕获该级别吗?

如下所示,即使是try块中抛出的异常也不会被捕获,因此不会被发送到事件日志。

如何使用Linq to Entities完成此操作?

代码:

public override string ResetPassword(string username, string answer)
{
 if (!EnablePasswordReset)
 {
  throw new NotSupportedException("Password reset is not enabled.");
 }

 if (answer == null && RequiresQuestionAndAnswer)
 {
  UpdateFailureCount(username, "passwordAnswer");

  throw new ProviderException("Password answer required for password reset.");
 }

 string newPassword =
   System.Web.Security.Membership.GeneratePassword(newPasswordLength, MinRequiredNonAlphanumericCharacters);


 ValidatePasswordEventArgs args =
   new ValidatePasswordEventArgs(username, newPassword, true);

 OnValidatingPassword(args);

 if (args.Cancel)
  if (args.FailureInformation != null)
   throw args.FailureInformation;
  else
   throw new MembershipPasswordException("Reset password canceled due to password validation failure.");


 OdbcConnection conn = new OdbcConnection(connectionString);
 OdbcCommand cmd = new OdbcCommand("SELECT PasswordAnswer, IsLockedOut FROM Users " +
    " WHERE Username = ? AND ApplicationName = ?", conn);

 cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username;
 cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = pApplicationName;

 int rowsAffected = 0;
 string passwordAnswer = "";
 OdbcDataReader reader = null;

 try
 {
  conn.Open();

  reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

  if (reader.HasRows)
  {
   reader.Read();

   if (reader.GetBoolean(1))
    throw new MembershipPasswordException("The supplied user is locked out.");

   passwordAnswer = reader.GetString(0);
  }
  else
  {
   throw new MembershipPasswordException("The supplied user name is not found.");
  }

  if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
  {
   UpdateFailureCount(username, "passwordAnswer");

   throw new MembershipPasswordException("Incorrect password answer.");
  }

  OdbcCommand updateCmd = new OdbcCommand("UPDATE Users " +
   " SET Password = ?, LastPasswordChangedDate = ?" +
   " WHERE Username = ? AND ApplicationName = ? AND IsLockedOut = False", conn);

  updateCmd.Parameters.Add("@Password", OdbcType.VarChar, 255).Value = EncodePassword(newPassword);
  updateCmd.Parameters.Add("@LastPasswordChangedDate", OdbcType.DateTime).Value = DateTime.Now;
  updateCmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username;
  updateCmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = pApplicationName;

  rowsAffected = updateCmd.ExecuteNonQuery();
 }
 catch (OdbcException e)
 {
  if (WriteExceptionsToEventLog)
  {
   WriteToEventLog(e, "ResetPassword");

   throw new ProviderException(exceptionMessage);
  }
  else
  {
   throw e;
  }
 }
 finally
 {
  if (reader != null) { reader.Close(); }
  conn.Close();
 }

 if (rowsAffected > 0)
 {
  return newPassword;
 }
 else
 {
  throw new MembershipPasswordException("User not found, or user is locked out. Password not Reset.");
 }
}

1 个答案:

答案 0 :(得分:1)

我想我已经明白了。使用Linq to Entities,您可以捕获EntityException。这将是上述代码中OdbcException的等效项。

感谢任何花时间在此的人。