Entity Framework Code First ASP.Net and DbContext

4

我是论坛的长期潜水者,我有一个问题,基于MVC音乐商店教程 (http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4), 我正在跟随这个教程。

该教程使用 EF Code First 来设置 CE 数据库。但是有三个模型 Album、Genre 和 Artist 作为类文件。现在 1 Album 可以有很多 Artis,然而代码只提到了 Genre 和 Artist:

using System.Data.Entity;

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

为什么这段代码没有提到 say:
public DbSet<Artist> Artists { get; set; }

感谢阅读。希望我没有表达得太蠢。


只有在希望直接查询时,才需要公开一个 DbSet - Dismissile
1个回答

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

谢谢,我不确定是否正确,等StackOverflow允许后我会标记为正确答案! - Tom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接