实体框架代码首先是ASP.Net和DbContext

时间:2012-04-17 11:17:21

标签: c# asp.net-mvc entity-framework code-first dbcontext

论坛的长时间潜伏,我有一个基于MVC音乐商店教程的问题(http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part- 4)我正在关注。

本教程使用EF Code First来设置CE数据库。然而,Album,Genre和Artist有三个模型作为类文件。现在1专辑可以有很多艺术家,但代码只提到了流派和艺术家:

using System.Data.Entity;

namespace MvcMusicStore.Models
{
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
    }
}

为什么这段代码没有提到:

public DbSet<Artist> Artists { get; set; }

感谢阅读。我希望我不是太傻了。

1 个答案:

答案 0 :(得分:0)

因为相册中有艺术家,所以我们可以按专辑访问艺术家:

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; }
    }
}