2015-02-10 104 views
0

我想使用下面的實體框架代碼更新表中的字段,但它似乎不會修改該字段。這真令人沮喪,所以我想知道是否有人能告訴我我做錯了什麼?使用實體框架更新表

default.aspx.vb:

Protected Sub btn_Save_Click(sender As Object, e As System.EventArgs) Handles btn_Save.Click 
     SaveNewsBox() 
    End Sub 

    Private Sub GetNewsBox() 
     Dim newsBox As GLC.Home = BLL.Homes.Homes.GetNewsBox() 
     If newsBox IsNot Nothing Then 
      txt_NewsBox.Text = newsBox.NewsBox 
     End If 
    End Sub 

    Private Sub SaveNewsBox() 

     Dim newsBox As New GLC.Home 

     newsBox.NewsBox = txt_NewsBox.Text 

     If BLL.Homes.Homes.Update(newsBox) Then 
      Master.AlertStyle = "alert-success" 
      Master.AlertMessage = "<i class=""fa fa-thumbs-o-up""></i> Meal details saved, <a href=""/secure/"">return to main menu.</a>" 
      Master.AlertVisible = True 
     Else 
      Master.AlertStyle = "alert-danger" 
      Master.AlertMessage = "<i class=""fa fa-thumbs-o-down""></i> Warning news box details could not be saved.</a>" 
      Master.AlertVisible = True 
     End If 

    End Sub 
End Class 

Homes.vb:

Imports DAL 
Imports GLC 

Namespace Homes 
    Public Class Homes 
     Public Shared Function GetNewsBox() As Home 
      Return MethodClasses.HomesHandler.GetNewsBox() 
     End Function 

     Public Shared Function Update(newsBox As Home) As Boolean 
      Return MethodClasses.HomesHandler.Update(newsBox) 
     End Function 

    End Class 
End Namespace 

HomesHandler.vb:

Imports GLC 
Imports System.Linq.Dynamic 

Namespace MethodClasses 
    Public Class HomesHandler 

     Public Shared Function GetNewsBox() As Home 
      Using context As New GLCContext 
       Try 
        Return context.Homes.Single() 
       Catch ex As Exception 
        Return Nothing 
       End Try 
      End Using 
     End Function 

     Public Shared Function Update(newsBox As Home) As Boolean 
      Dim newsBoxUpdated As Boolean = False 
      Using context As New GLCContext 
       Try 
        context.Homes.Attach(newsBox) 
        Dim entry = context.Entry(newsBox) 
        entry.State = EntityState.Modified 

        context.SaveChanges() 
        newsBoxUpdated = True 

       Catch ex As Exception 
        newsBoxUpdated = False 
       End Try 
      End Using 

      Return newsBoxUpdated 

     End Function 

    End Class 
End Namespace 
+0

沒有錯誤?至少你似乎沒有提供newsBox的PK值,或者總是等於0的值。 – tschmit007 2015-02-10 13:02:50

+0

如果你從Update()函數中刪除了try/catch,你會得到一個錯誤嗎?另外,你能分享你的模式嗎? – theduck 2015-02-10 17:53:15

回答

0

我猜發生的情況是,當你調用Dim entry = context.Entry(newsBox)它拋出一個異常並返回false,因爲這個對象是你在aspx中創建的新對象,它無法找到它。

看起來您的表中只有一個條目(或者您只提供了獲取第一行的方法)。如果是這樣的話,我只是這樣做:

Public Shared Function Update(newsBox As Home) As Boolean 
      Dim newsBoxUpdated As Boolean = False 
      Using context As New GLCContext 
       Try 
        Dim entry = GetNewsBox() 
        entry.NewsBox = newsBox.NewsBox 

        context.SaveChanges() 
        newsBoxUpdated = True 

       Catch ex As Exception 
        newsBoxUpdated = False 
       End Try 
      End Using 

      Return newsBoxUpdated 

     End Function 

上下文就知道實體被修改(因爲你改變了它,而它附後)。如果你不用鍵就調用Attach()(或者用EF來標識給定對象的特定行),那麼它將最終插入一個新行,但你可能永遠不會看到它,因爲你只有Single( )。

如果這真的是您修改的唯一屬性,只需將文本框的值作爲字符串傳遞到Update函數而不是傳遞一個新的空(除了單個字段),您就可以節省一些開銷並簡化操作,目的。