Creating Model relation in ASP.NET MVC 5 in VB

asp.net-mvc-5 entity-framework-6 vb.net

Question

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

1
3
10/28/2016 1:45:49 PM

Accepted 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
1
10/28/2016 3:09:49 PM

Popular Answer

Thanks Hackerman!, this also works refactoring only Bands class. But i dont know if this way misses something important you did in your code.

Public Class Band
   Public Property BandID() As Integer
   Public Property BandName() As String
   Public Property MusicStyleID() As Integer
   Public Property MusicStyleRef() As MusicStyle
End Class

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow