日期:2014-05-20  浏览次数:20709 次

EntitySet调用add方法去没有改变Untracked state
EntitySet 调用add方法后没有将object state 从Untracked 修改为可被datacontext跟踪的状态,导致
EntitySet add一个新的Entity或修改里面的一个实体 调用submitchanges后发现数据库没有保存更改
下面试代码:

初始化:
  IEnumerable<T_XEStudent> students = context.GetTable<T_XEStudent>();
  string studentName = this.cmbStudentName.Text;
  CurrentSelectedStudent = students.SingleOrDefault(m => m.StudentName.Equals(studentName));
  currentPaper = CurrentSelectedStudent.T_XEPapers.SingleOrDefault();

  private void btnSavePingYue_Click(object sender, EventArgs e)
  {
  T_XEComment com=currentPaper.T_XEComments.SingleOrDefault(m => m.CommentTeacherName.Equals(teacher.TeacherName));
  //修改评语 数据库中友Comment记录 
  if (com!=null)
  {  
  com.ReceiveScoreSelectSubject = Convert.ToInt32(this.cmbReceiveScoreSelectSubject.Text);
  com.RecieveScoreComprehensiveSkill = Convert.ToInt32(this.cmbRecieveScoreComprehensiveSkill.Text);
  com.RecieveScoreReferenceDocument = Convert.ToInt32(this.cmbRecieveScoreReferenceDocument.Text);
  com.RecieveScoreWritingLevel = Convert.ToInt32(this.cmbRecieveScoreWritingLevel.Text);
  com.RecieveScoreStudingLevel = Convert.ToInt32(this.cmbRecieveScoreStudingLevel.Text);
  com.RecieveScoreFormat = Convert.ToInt32(this.cmbRecieveScoreFormat.Text);
  com.CommentContent = this.txtBoxComment.Text;
  if (teacherService.SavePingYue()) //修改记录后保存 调用public bool SaveChanges()方法
  {
  MessageBox.Show("修改评阅内容成功");
  }
  else
  {
  MessageBox.Show("修改评阅内容失败");
  }
   
  }
  //增加评语
  else
  {
  //跟对象必须做出修改 新添加的子对象才能被DataContext识别
  com = new T_XEComment();
  com.CommentTeacherName = teacher.TeacherName;
  com.ReceiveScoreSelectSubject = Convert.ToInt32(this.cmbReceiveScoreSelectSubject.Text);
  com.RecieveScoreComprehensiveSkill = Convert.ToInt32(this.cmbRecieveScoreComprehensiveSkill.Text);
  com.RecieveScoreReferenceDocument = Convert.ToInt32(this.cmbRecieveScoreReferenceDocument.Text);
  com.RecieveScoreWritingLevel = Convert.ToInt32(this.cmbRecieveScoreWritingLevel.Text);
  com.RecieveScoreStudingLevel = Convert.ToInt32(this.cmbRecieveScoreStudingLevel.Text);
  com.RecieveScoreFormat = Convert.ToInt32(this.cmbRecieveScoreFormat.Text);
  com.CommentContent = this.txtBoxComment.Text;
   
  currentPaper.T_XEComments.Add(com);//调用EntitySet的Add()方法

  if (teacherService.SavePingYue()) //调用public bool SaveChanges()方法
  {
  MessageBox.Show("增加评阅内容成功");
  }
  else
  {
  MessageBox.Show("增加评阅内容失败");
  }
  }
  }

  public bool SaveChanges()
  {
  try
  {
  ChangeSet set = context.GetChangeSet();
  IList<object> updateSet = set.Updates;
  IList<object> delelteSet= set.Deletes;