论坛的长期潜伏者,我有一个基于 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/