我的数据库是由生成的。我有3种型号,包括板,线和帖子。我没有手动跟踪所有的parent_id值,而是用一些ICollections创建了它们,以便EF为我做到这一点。
然而,当我在一个线程/查看/5页时,我想要能够获取它的母板Id,但我不知道如何。但是,该列确实存在于数据库中,因为所有EF都是在创建线程并将其添加到Board_Id ICollection时创建一个ICollection字段并填充它。
这是我的密码:
// Board model, containing a title and a collection of threads
public class Board
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Thread> Threads { get; set; }
}
// Thread model, containing a title and a collection of posts
public class Thread
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}现在,当我在线程/视图操作中时,如何从列Board_Id中获取值?
// GET: Threads/View/5
[AllowAnonymous]
public ActionResult View(int? id) {
Thread thread = db.Threads.Find(id);
// Fetch the parent board Id
int boardId = ...;
return View(thread);
}发布于 2014-07-16 07:23:19
您需要线程类中的Board属性。财产将是
public virtual Board Board {get; set;}有了该属性,您将能够从线程导航到主板
https://stackoverflow.com/questions/24774507
复制相似问题