FreeOZ论坛

标题: Entity 外键问题求助 [打印本页]

作者: xblues    时间: 23-4-2010 09:32
提示: 作者被禁止或删除, 无法发言 标题: Entity 外键问题求助
请注意看下面两句红色的代码,第一行长的是书上的代码,第二行是我觉得也可以用的代码。

既然我从 item.CategoryId里面已经可以直接查到这个ID了,为什么作者却绕这么大一个弯路来得到这个CategoryID呢?

  1.         public ActionResult Details(int id)
  2.         {
  3.             Post item = dbentities.Posts.Where(i => i.Id == id).FirstOrDefault();

  4.             int categoryID = (int)item.CategoriesReference.EntityKey.EntityKeyValues.First(k => k.Key == "Id").Value;

  5.             //int categoryID = item.CategoryId;

  6.             string categoryName = (string)dbentities.Categories.First(m => m.Id == categoryID).Title;

  7.             ViewData["category"] = categoryName;

  8.             return View(item);
  9.         }
复制代码

作者: xblues    时间: 23-4-2010 09:35
提示: 作者被禁止或删除, 无法发言 标题: 作者还加了这个链接注释
http://social.msdn.microsoft.com ... 6-a4dd-54e1c08958d9




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) Powered by Discuz! X3.2