Vb.net ASP.NET MVC音乐商店问题
在MVC3MusicStore示例中,他们执行以下操作Vb.net ASP.NET MVC音乐商店问题,vb.net,entity-framework-4.1,Vb.net,Entity Framework 4.1,在MVC3MusicStore示例中,他们执行以下操作 Public Class Album Public Property GenreId AS Integer ... Public Overidable Property Genre as Genre End Class 实体框架看起来非常灵活,并且发现GenreId是流派表的主键。因此,Album的SQL表将GenreId作为一列,当我们加载Album storeDB.Album.findx时,它也将加载流派实体 我的问题是,如果
Public Class Album
Public Property GenreId AS Integer
...
Public Overidable Property Genre as Genre
End Class
实体框架看起来非常灵活,并且发现GenreId是流派表的主键。因此,Album的SQL表将GenreId作为一列,当我们加载Album storeDB.Album.findx时,它也将加载流派实体
我的问题是,如果我们想要第二个相同类型的对象,该怎么办。例如,如果每张专辑都有一个主要流派和一个次要流派呢。我想我们可以这样做
Public Class Album
Public Property MainGenreId As Integer
Public Overidable Property MainGenre As Genre
Public Property BackupGenreId As Integer
Public Overidable Property BackupGenre as Genre
End Class
我也试过这个
Public Class Album
Public Property MainGenre_GenreId As Integer
Public Overidable Property MainGenre As Genre
Public Property BackupGenre_GenreId As Integer
Public Overidable Property BackupGenre as Genre
End Class
但是,这些选项都不能正常工作。有人能告诉我正确的方向吗?这样当我通过storeDB.album.findx加载相册时,它会自动填充主流派和备份流派?谢谢。您需要使用数据注释或Fluent API来配置关系。假设在类型实体上有两个名为MainGenreAlbums和BackupGenreAlbums的集合属性,则可以使用ForeignKey和InverseProperty属性来配置关系
Public Class Album
Public Property MainGenreId As Integer
<ForeignKey("MainGenreId")>
<InverseProperty("MainGenreAlbums")>
Public Overidable Property MainGenre As Genre
Public Property BackupGenreId As Integer
<ForeignKey("BackupGenreId")>
<InverseProperty("BackupGenreAlbums")>
Public Overidable Property BackupGenre as Genre
End Class
杰出的谢谢,效果很好。我想弄清楚Fluent的API,但是DA很好。我没有使用InverseProperty,因为我不太明白它是什么,但它只使用指向适当整数的ForeignKey。再次感谢。