2016-11-30 96 views
0

我有一个挑战,我试图从数据库更新记录但它没有保存到数据库,并没有显示任何错误,它给出了一条消息,它已更新记录但数据库没有变化。我的代码如下。任何建议实体框架不保存到数据库

dbEntities context = new dbEntities(); 

var query = context.ConsultantsProfiles.SingleOrDefault(c => c.Username == username); 

     if (query != null) 
     { 

      query.Summary = txtSummary.Text; 
      query.CareerTitle = txtTitle.Text; 
      query.ConsultantType = cbType.Text; 
      query.Username = username; 
      query.FirstName = txtFirstname.Text; 
      query.LastName = txtLastName.Text; 
      query.Email = txtEmail.Text; 
      query.DateofBirth = Convert.ToDateTime(dptDateofBirth.Value); 
      query.PhoneNumber = txtPhoneNumber.Text; 
      query.Website = txtWebsite.Text; 
      query.Town = txtTown.Text; 
      query.Country = txtCountry.Text; 

      if (FileUpload1.HasFile) 
      { 
      //image upload 
      HttpPostedFile postedFile = FileUpload1.PostedFile; 
      // HttpPostedFile postedFile = uploadControl.UploadedFiles[i]; 
      Stream stream = postedFile.InputStream; 
      BinaryReader reader = new BinaryReader(stream); 
      byte[] imgByte = reader.ReadBytes((int)stream.Length); 
      int imglength = FileUpload1.PostedFile.ContentLength; 

      query.ProfilePhoto = imgByte; 

      } 



      context.ConsultantsProfiles.Attach(query); 
      context.Entry(query).State = EntityState.Modified; 
      context.SaveChanges(); 
     } 
     Response.Write("<script language=javascript>alert('Notification: The Profile Has been Updated');</script>"); 
     } 
+0

你有没有做任何调试?例如,当'query == null'时,你有一个被排除的块。你有没有检查是否可能是这种情况?或者你有没有监视你的数据库查询来查看执行了什么?还是别的什么可能会给你任何想法? – hvd

+0

欢迎来到堆栈溢出!它看起来像你需要学习使用调试器。请帮助一些[互补调试技术](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之后仍然有问题,请随时返回更多详情。 –

回答

0
dbEntities context = new dbEntities(); 
var consultantProfile = new ConsultantProfile 
{ 
    Summary = txtSummary.Text; 
    CareerTitle = txtTitle.Text; 
    ConsultantType = cbType.Text; 
    Username = username; 
    FirstName = txtFirstname.Text; 
    LastName = txtLastName.Text; 
    Email = txtEmail.Text; 
    DateofBirth = Convert.ToDateTime(dptDateofBirth.Value); 
    PhoneNumber = txtPhoneNumber.Text; 
    Website = txtWebsite.Text; 
    Town = txtTown.Text; 
    Country = txtCountry.Text; 
} 
if (FileUpload1.HasFile) 
{ 
    //image upload 
    HttpPostedFile postedFile = FileUpload1.PostedFile; 
    // HttpPostedFile postedFile = uploadControl.UploadedFiles[i]; 
    Stream stream = postedFile.InputStream; 
    BinaryReader reader = new BinaryReader(stream); 
    byte[] imgByte = reader.ReadBytes((int)stream.Length); 
    int imglength = FileUpload1.PostedFile.ContentLength; 

    consultantProfile.ProfilePhoto = imgByte; 
} 
context.Entry(ConsultantsProfiles).State = EntityState.Modified; 
context.SaveChanges();