2016-10-28 59 views
3

我創建了兩個簡單的模型「MusicStyle」和「」,我想一個MusicStyle。所有的例子,我可以找到在C#中,我不知道如何在Visual Basic中正確的語法。創建模型的關係在VB

這是我的代碼爲類/模型。

 

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 
 

出於某種原因,這產生一個FK在MusicStyle表代替波段表,並在視圖的FK沒有HTML控件。

Tables and Relations

回答

1

你需要重構代碼,以實現正確的關係:

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 
0

感謝Hackerman!這也適用重構只類。但我不知道這種方式是否遺漏了你在代碼中做過的重要事情。

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 
+0

是的,但我認爲這不能正確反映場景......基本上在我的回答中,您定義了「一個樂隊有音樂風格」和「一個音樂風格可以由多個樂隊共享」的關係。 ..因此,它更易於閱讀,因爲它能以兩種方式反映這種關係。 – Hackerman