使用相关表中的主键填充外键

时间:2012-03-23 23:38:34

标签: c# asp.net asp.net-mvc-3 linq-to-sql

我有以下代码:

var game = (from k in db.GamerTBLs
   where k.UserName == User.Identity.Name
   select k.GamerID).Single();

return View(game);

我无法让我的Game表从其主表Gamer中填充外键,Gamer具有1对多的关系:1个玩家可以有很多游戏。

我的创建功能是这样的:

[HttpPost]
public ActionResult Create(GameTBL gametbl)
{
    if (ModelState.IsValid)
    {
        db.GameTBLs.Add(gametbl);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }


    var game = (from k in db.GamerTBLs where k.UserName == User.Identity.Name
        select k.GamerID).Single();

    return View(game);
}

但是当我运行应用程序时,游戏表不显示外键,因为该框显示该字段为空白。

如果您需要更多信息,请告诉我。

0 个答案:

没有答案