2016-10-28 12 views
3

でASP.NET MVC 5におけるモデルの関係を作成します。私が見つけることができるすべての例はC#であり、Visual Basicで正しい構文がどのようになっているかわかりません。は、私は2つの単純なモデル「<strong>MusicStyle</strong>」を作成し、「<strong>バンド</strong>」、と私は<strong>バンド</strong>が<strong>MusicStyle</strong>を持っていると思いますよ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 
 

これは代わりにMusicStyle表にバンドテーブル、およびFKのビューでノーHTMLコントロールをFKを生成し、いくつかの理由。あなたが右の関係を達成するために、あなたのコードをリファクタリングする必要が

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

ええ、これはシナリオを正しく反映していないと思います。基本的に私の答えでは、「One Bandは音楽スタイルを持っています」と「One Music Styleは複数のバンドで共有できます。両方の方法で関係を反映させるので、読みやすいです。 – Hackerman

関連する問題

 関連する問題