c# - Entity Framework 代码优先 ASP.Net 和 DbContext

标签 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 数据库。然而,有三种模型专辑、流派和艺术家作为类文件。现在 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; }

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

最佳答案

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

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

关于c# - Entity Framework 代码优先 ASP.Net 和 DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10190196/

相关文章:

c# - 有没有办法在 Medium Trust 托管上对文档进行数字签名?

c# - tostring ("000.0") 生成错误值

asp.net-mvc - Https:从 http 隐藏 Controller 方法

c# - EF Core - 无法添加具有相同相关对象的实体

c# - 如果为空,如何使用 json.net 忽略类中的属性

C# FileInfo 数组到字符串数组

javascript - 在不广播 Clients.All.someMethod() 和炸毁客户端资源的情况下确定 Signal R 连接状态

asp.net - 我如何处理和缩小 asp.net MVC 中的 GZipped 表单帖子?

javascript - 使用 BreezeJS 获取 DateTime 类型的日期

c# - Entity Framework 查询得到改进