迭代我使用这些模型:加入许多一对多模式,允许在视图
public class FList
{
public int FListID { get; set; }
public string Title { get; set; }
public int UserID { get; set; }
public DateTime Posted { get; set; }
public virtual User User { get; set; }
public ICollection<FListItem> Items { get; set; }
}
public class Item
{
public int ItemID { get; set; }
public string Name { get; set; }
public ICollection<FListItem> FLists { get; set; }
}
public class FListItem
{
public int FListID { get; set; }
public int ItemID { get; set; }
public int Score { get; set; }
public virtual FList FList { get; set; }
public virtual Item Item { get; set; }
}
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public ICollection<FList> FLists { get; set; }
}
与上创建FListItem复合主键此流利的API。
modelBuilder.Entity<FaveListItem>().HasKey(fi => new { fi.FaveListID, fi.ItemID });
modelBuilder.Entity<FList>()
.HasMany(f => f.Items)
.WithRequired(fi => fi.FList)
.HasForeignKey(fi => fi.FListID);
modelBuilder.Entity<Item>()
.HasMany(f => f.FLists)
.WithRequired(fi => fi.Item)
.HasForeignKey(fi => fi.ItemID);
如果我添加控制器,用于FList
,下面的脚手架创建
public ActionResult Index()
{
var fLists = db.FLists.Include(f => f.User);
return View(fLists.ToList());
}
其允许fLists
要在索引视图迭代。
我需要做的是包括Items
可以迭代通过每个fList
。我无法使用Include
,因为FList
上没有Items
导航属性。
我想我需要在fList
Join
Items
,并创建一个视图模型与一个IEnumerable允许的Items
迭代。
请有人建议,如果上述推理是正确的,如果是这样,请协助Join
?