2012-05-14 50 views
1

我正在學習asp.net MVC。使用Code First模型創建您的類,然後使用EF來管理所有數據庫交互。只更新一個字段的子集

有沒有反正只使用這種方法從給定的表中請求一些字段,只更新這些字段?

我的整個類是:

Public Class Employee 
    Public Property ID() As Integer 
    <DisplayName("Staff Name")> 
    <Required()> 
    Public Property StaffName() As String 
    <DisplayName("Location")> 
    <Required()> 
    Public Property Location() As String 
    <Required()> 
    <DisplayName("Team Leader")> 
    Public Property teamleader() As String 

    Public Property ScoreCardM1() As Integer 
    Public Property ScoreCardM1Notes() As String 
    Public Property ScoreCardM2() As Integer 
    Public Property ScoreCardM2Notes() As String 
    Public Property ScoreCardM3() As Integer 
    Public Property ScoreCardM3Notes() As String 
    Public Property ScoreCardM4() As Integer 
    Public Property ScoreCardM4Notes() As String 
    Public Property ScoreCardM5() As Integer 
    Public Property ScoreCardM5Notes() As String 
    Public Property ScoreCardM6() As Integer 
    Public Property ScoreCardM6Notes() As String 
    Public Property ScoreCardTotal() As Integer 
    Public Property ScoreCardTotalNotes() As String 
    Public Property ScoreCardM7() As Integer 
    Public Property ScoreCardM7Notes() As String 
    Public Property ScoreCardM8() As Integer 
    Public Property ScoreCardM8Notes() As String 
    Public Property ScoreCardM9() As Integer 
    Public Property ScoreCardM9Notes() As String 
    Public Property ScoreCardQ3Total() As Integer 
    Public Property ScoreCardQ3TotalNotes() As String 
    Public Property ScoreCardM10() As Integer 
    Public Property ScoreCardM10Notes() As String 
    Public Property ScoreCardM11() As Integer 
    Public Property ScoreCardM11Notes() As String 
    Public Property ScoreCardM12() As Integer 
    Public Property ScoreCardM12Notes() As String 
    Public Property ScoreCardQ4Total() As Integer 
    Public Property ScoreCardQ4TotalNotes() As String 
    Public Property GeneralNotes() As String 
End Class 

不過,我只想顯示,每次編輯一個月份:

Public Class Employee 
    Public Property ID() As Integer 
    <DisplayName("Staff Name")> 
    <Required()> 
    Public Property StaffName() As String 

    Public Property ScoreCardM1() As Integer 
    Public Property ScoreCardM1Notes() As String 
End Class 

我最好如何實現這一目標?我是否需要設置另外12個類,每個類只包含一個月,還是在數據庫行中更新字段子集的最佳實踐方式,而不影響其他類?

感謝您的幫助,

馬克

+0

嗨 - 我可能在這裏找到答案:http://stackoverflow.com/questions/6200901/updating-only-part-of-a-model-我沒有看到什麼時候搜索。如果有人對最佳實踐有任何其他建議,請告訴我們 - 再次感謝Mark – Mark

+0

鏈接的答案建議爲編輯後的記錄重新查詢數據庫,並使用傳遞到控制器的模型更新它。這好像你正在做一個額外的查詢(重新選擇要更新的記錄)。這真的是實現這一目標的最佳方式嗎?謝謝,馬克 – Mark

+0

你總是可以創建一個月類,並添加一個新的屬性爲月份的名稱。然後,您可以在員工課堂中收集月份報告。 – keshav

回答

0

進一步上面我的評論....你可以設置你的類如下:

Employee類:

Public Class Employee 
    Public Property EmployeeID() As Integer 
    <DisplayName("Staff Name")> 
    <Required()> 
    Public Property StaffName() As String 
    <DisplayName("Location")> 
    <Required()> 
    Public Property Location() As String 
    <Required()> 
    <DisplayName("Team Leader")> 
    Public Property teamleader() As String 
    Public virtual Property reports() As ICollection<Of MonthlyRports> // not sure if the syntax is right (haven't worked in VB) 
End Class 

MonthlyReport Class

Public Class Employee 
    Public Property MonthlyReportID() As Integer // primary key 
    Public Property EmployeeID() As Integer // foreign key. who this report belongs to 
    Public Property Month As String // report for month ... 
    Public Property ScoreCard() As Integer 
    Public Property ScoreCardNotes() As String 
End Class 
+0

乾杯Keshav,馬克 – Mark

相關問題