以下项目将在Microsoft SQL Server数据库中维护,并首先使用EF代码进行开发:
定义嵌套类是否总是更好,意思如下(缩写定义以保持简单),让数据库完成维护关系的工作:
Public Class Assembly
Public Property assemblyID As Integer
Public Property parts As New List(Of Part)
End Class
Public Class Part
Public Property partID As Integer
Public Property subitems As New List(Of Subitem)
End Class
Public Class Subitem
Public Property subitemID As Integer
Public Property components As New List(Of Component)
End Class
Public Class Component
Public Property componentID As Integer
Public Property elements As New List(Of Element)
End Class
Public Class Element
Public Property elementID As Integer
Public Property Name As New List(Of String)
End Class
或者是否有任何理由将这些类分开并进行手动工作以维护每个类中记录之间的关系,如下所示:
Public Class Assembly
Public Property assemblyID As Integer
Public Property parts As New List(Of Integer) 'which would be partIDs
End Class
Public Class Part
Public Property partID As Integer
Public Property subitems As New List(Of Integer) 'which would be subitemIDs
End Class
Public Class Subitem
Public Property subitemID As Integer
Public Property components As New List(Of Integer) 'which would be componentIDs
End Class
Public Class Component
Public Property componentID As Integer
Public Property elements As New List(Of Integer) 'which would be elementIDs
End Class
Public Class Element
Public Property elementID As Integer
Public Property Name As New List(Of String)
End Class
我一直认为我们应该设计嵌套的类来遵循实际数据的结构。但由于这是一个更深层的嵌套,我想我会问,如果有其他设计方法我应该考虑。
在这种特殊情况下,所有记录都是唯一的。 I.E.即使我让它看起来像是一个组装部分,我只是试图清楚地命名每个级别。但在这种情况下,这些图层项中的任何一个都不会在不同的程序集中使用。
答案 0 :(得分:1)
如果您的所有结构都具有相同的深度,那么您的模型可能是最好的。否则考虑层次结构。这样的事情。
Public Class PartModel
Public Property Id as Integer
Public Property Name As String
Public Property ParentId As Integer? ''Nullable(Of Integer)
<ForeignKey("ParentId")>
Public Property SubParts As List(Of PartModel) ''Note no **New** here. New will be in a Controller
End Class