Gonzalo Hevia Castillo Gonzalo Hevia Castillo - 1 month ago 12
Vb.net Question

Creating Model relation in ASP.NET MVC 5 in VB

I'm creating two simple models "MusicStyle" and "Bands", and i want a Band to have a MusicStyle. All the examples i can find are in C# and i can't figure out how is the correct syntax in Visual Basic.

Here is my code for both Classes/Models.



Public Class MusicStyle
Public Property MusicStyleID() As Integer
Public Property MusicStyleName() As String
End Class





Public Class Bands
Public Property BandsID() As Integer
Public Property BandsName() As String
Public Overridable Property MusicStyleID() As ICollection(Of MusicStyle)
End Class



For some reason this generates an FK in the MusicStyle Table instead Bands table, and no HTML controls in the Views for the FK.

Tables and Relations

Answer

You need to refactor your code in order to accomplish the right relationship:

Public Class Band
   Public Property BandID() As Integer
   Public Property BandName() As String
   'This is the FK
   Public Property MusicStyleID() As Integer
   <ForeignKey("MusicStyleID")> 
   Public Property MusicStyleRef() As MusicStyle
End Class

Public Class MusicStyle
   Public Property MusicStyleID() As Integer
   Public Property MusicStyleName() As String
   Public Overridable Property Bands() As ICollection(Of Band)
End Class