2016-11-30 5 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(); 
関連する問題