Viewmodels and DBs have nothing to do with each other. That's your first problem, you're trying to make them related. If you're using a viewmodel in your UI then you'll need to map your DB structures to it before passing it to the client and any models that come back from the client need to be converted back to whatever structure your data layer requires. You have to write this mapping yourself (or use a third party mapping tool like Automapper). For simple apps viewmodels may be overkill but as apps get larger viewmodels become very useful for maintainability.
Public Class CODE_OPINION_TBL
Public Property ID() As Integer
Public Property Chapter() As String
Public Property Section() As String
End Class
Public Class ViewModelClass_CodeOpinions
Public Property ID() As Integer
Public Property Chapter() As String
Public Property Section() As String
End Class
' I prefer to use extension methods or mapper classes for conversion '
Public Shared Class CodeOpinionMapper
<Extension()>
Public Shared Function ToViewModel ( data As CODE_OPINION_TBL ) As ViewModelClass_CodeOpinions
Dim model As new ViewModelClass_CodeOpinions
With model
.ID = data.ID
.Chapter = data.Chapter
.Section = data.Section
End With
Return model
End Function
<Extension()>
Public Shared Function ToData ( model As ViewModelClass_CodeOpinions ) As CODE_OPINION_TBL
Dim data As new CODE_OPINION_TBL
With data
.ID = model.ID
.Chapter = model.Chapter
.Section = model.Section
End With
Return data
End Function
End Class
Dim data As New CODE_OPIONION_TBL
Dim model = data.ToViewModel()
Dim newData = model.ToData()
To be honest your specific example isn't complex enough to justify viewmodels so it is hard to see the benefit. The primary benefit of a viewmodel (or any mapping) is to reformat the underlying data into a structure that can be more easily processed by whoever you're handing it to (a view in this case). A more complex example may make this clearer.
Public Class StateOrProvince
Public Property ID() As Integer
Public Property Name() As String
'Typically a data model has an ID and the corresponding object, if any '
Public Property CountryID() As Integer
Public Property Country() As Country
End Class
Public Class Country
Public Property ID() As Integer
Public Property Name() As String
End Class
Public Class StateOrProvinceViewModel
Public Property ID() As Integer
Public Property Name() As String
'Flatten country information '
Public Property CountryId() As Integer
Public Property CountryName() As String
End Class
Public Shared Class StateOrProvinceMapper
<Extension()>
Public Shared Function ToViewModel ( state As StateOrProvince ) As StateOrProvinceViewModel
Dim model As New StateOrProvinceViewModel()
With model
.ID = state.ID
.Name = state.Name
End With
If state.Country Is Not Nothing Then
model.CountryID = state.Country.ID
model.CountryName = state.Country.Name.ToUpperCase()
End If
Return model
End Function
<Extension()>
Public Shared Function ToData ( model As StateOrProvinceViewModel ) As StateOrProvince
Dim data As New StateOrProvince
With data
.ID = model.ID
.Name = model.Name
.CountryID = model.CountryID
End With
' Often we do not need related objects when going back to DB so leave the Country off... '
Return data
End Function
End Class