我看过一些在这里:http://www.asp.net/mvc/tutorials/mvc-music-store-part-4并设置一些卡ASP.NET MVC如何链接2模型 - 需要一些解释?
namespace MvcMusicStore.Models
{
public class Artist
{
public int ArtistId { get; set; }
public string Name { get; set; }
}
}
namespace MvcMusicStore.Models
{
public class Album
{
public int AlbumId { get; set; }
public int GenreId { get; set; }
public int ArtistId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string AlbumArtUrl { get; set; }
public Genre Genre { get; set; }
public Artist Artist { get; set; }
}
}
namespace MvcMusicStore.Models
{
public partial class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
}
专辑中的类,为什么他们有:
public Genre Genre { get; set; }
public Artist Artist { get; set; }
而在流派班,为什么他们有:
public List<Album> Albums { get; set; }
你能解释为什么我们应该有这些领域?
你能告诉我怎么样“公开名单相册”,我看到一个类型可以有很多专辑,所以也许这是代码的想法,但我是新来的C#... –
nvcnvn
就是这样 - 一个类型可能有许多专辑,而'List'将保存该类型的所有专辑。由于每个专辑只有一个艺术家和流派,所以专辑只有一个'genre'属性和一个'Artist'属性。 –
ipr101