【问题标题】:Updating just a subset of fields in a row仅更新一行中的字段子集
【发布时间】:2012-05-14 09:37:23
【问题描述】:

我正在学习 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 个类,每个类只包含一个月,还是有一种最佳实践方法可以更新数据库行中的字段子集,而不影响其他类?

感谢您的帮助,

标记

【问题讨论】:

  • 嗨 - 我可能在这里找到了答案:stackoverflow.com/questions/6200901/… - 我在搜索时没有看到。如果有人对最佳实践有任何其他建议,请告诉我 - 再次感谢 Mark
  • 链接的答案建议重新查询数据库以获取已编辑的记录,并使用传递给控制器​​的模型对其进行更新。这似乎你正在做一个额外的查询(重新选择要更新的记录)。这真的是实现这一目标的最佳方式吗?谢谢,马克
  • 您始终可以只创建一个 Month 类并为月份名称添加一个新属性。然后您可以在您的员工类中收集月度报告。

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

关于我上面的评论......您可以按如下方式设置您的课程:

员工类:

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

月报类

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

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2014-09-08
    相关资源
    最近更新 更多