Linq to SQL轰炸DataContext

时间:2015-04-01 00:42:43

标签: c# linq-to-sql datacontext

出于某种原因,我的代码刚刚决定在进行构建后停止工作并且不确定问题是什么。

public class clsSaveError   {

  public clsSendEmail.clsSendEmail SendEmail = new clsSendEmail.clsSendEmail();
  public clsSettings.clsSEttings Settings = new clsSettings.clsSEttings();
  public clsLinqToSQL.K12DataClassesDataContext dbContext = new clsLinqToSQL.K12DataClassesDataContext();


    public void InsertErrorIntoDatabase(string monsterID, string dbtable, string errormessage, string stacktrace, EventLog log)
    {
        clsLinqToSQL.K12DataClassesDataContext dbContext = new clsLinqToSQL.K12DataClassesDataContext();


          try
          {
              dbContext.tblErrors err = new dbContext.tblErrors();
              err.monsterid = monsterID;
              err.dbtable = dbtable;
              err.errormessage = errormessage;
              err.stacktrace = stacktrace;
              dbContext.tblErrors.Insertonsubmit(err);
              dbContext.SubmitChanges();
          }
          catch (Exception except)
          {
              //write the error to the event log
              log.WriteEntry("ERROR! Unable to write error to database. Here are the details:" + "\n" +
                  "Exception Message: " + except.Message + "\n" +
                  "Exception Stacktrace: " + except.StackTrace + "\n" +
                  "Database Error Details: " + "\n" +
                  "Monster ID: " + monsterID + "\n" +
                  "Database Table: " + "\n" +
                  "Error Message: " + "\n" +
                  "Stacktrace: " + stacktrace);

              SendEmail.SendEmail(Settings.mailport, Settings.mailhost, Settings.mailtimeout, Settings.mailusername, Settings.mailpassword, Settings.mailfrom, Settings.mailto, "Database Error - Unable to update Error Log", "Message: " + except.Message + "\n" + "Stacktrace: " + except.StackTrace);
          }
    }

}

错误是:

'clsSaveError.clsSaveError.dbContext'是'字段',但用作“类型”

我知道这是一般性错误,但我不确定为什么它现在正在发生。感谢您的投入!

1 个答案:

答案 0 :(得分:1)

您在此处声明变量:

clsLinqToSQL.K12DataClassesDataContext dbContext = new clsLinqToSQL.K12DataClassesDataContext();

然后尝试将该变量用作此类型:

dbContext.tblErrors err = new dbContext.tblErrors();

如果dbContext是类型的名称,请不要使用相同的名称声明变量。如果它不是类型的名称,那么我不知道上面的代码行正在尝试做什么。

您的类型名称和变量名称完全破坏了C#约定,使代码难以阅读。例如:

public clsSendEmail.clsSendEmail SendEmail = new clsSendEmail.clsSendEmail();

您的类型名称是小写的,并且您的变量是大写的。这是标准惯例的落后。将cls添加到每个类型名称都不会有帮助。