我正在學習MVC 3,我遇到了一件事情。ASP.NET MVC3和實體框架 - 在一個視圖中的一對多關係
我在我的模型(圖庫和圖像)中有兩個表。這些表格與GalleryId(1圖庫 - 許多圖像)相關(一對多)。
在畫廊 - >詳細信息視圖我想插入畫廊的細節(這很簡單),還有一件事 - 從這個畫廊的圖像列表。我不知道該怎麼做。 這裏是班從這個模型:
public partial class Gallery
{
public Gallery()
{
this.Images = new HashSet<Image>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public System.DateTime CreatedOn { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public partial class Image
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public int GalleryId { get; set; }
public System.DateTime UploadedOn { get; set; }
public virtual Gallery Gallery { get; set; }
}
public partial class MyEntities : DbContext
{
public DbSet<Gallery> Galleries { get; set; }
public DbSet<Image> Images { get; set; }
}
什麼是做到這一點的最好方法是什麼?
這是一個關於實體框架的問題,而不是MVC – 2012-01-16 12:55:08
看起來更像NHibernate ....當你想要視圖上的圖像時,你不能做一個foreach循環,因爲Images是Gallery的一個屬性? foreach(在Galleries.Images var圖像)並通過圖像循環? – Matt 2012-01-16 13:03:16