日期:2014-05-20  浏览次数:20813 次

关于entity framework 外键问题(使用Linq-to-Entity)
最新在看entity framework的东西。发现生产的文件中的表都没有外键列!
然后查询一些资料,发现需要使用关联实体的方法来进行查询,如:
原来:product.CategoryID = 2; 
现在需要这样写:product.Category = ctx.Categories.First(c => c.ID == 2); 
也就说原来使用linq-to-sql 可以这样写:
var list = from c in ctx.Product
  where c.CategoryID == 2
  select c;
现在必须:
var list = from c in ctx.Product 
  where c.Category = ctx.Categories.FirstorDefault(c => c.ID == 2); 
  select c;
感觉比较麻烦。
第一个问题:有没有办法可以直接使用外键进行linq-to-entity查询。
第二个问题:使用关联实体的方法进行查询,是否当我查询Product的时候会同时查询Categories表。也就是说:
原来只需要使用
where CategoryID == 2。
现在变成 
where fk_ref = (select fk_ref from Category where CategoryID == 2) //注:fk_ref 为外键列

------解决方案--------------------
探讨
如果能选择是否根据外键引用在模型中生成列,请问在什么地方选择的?没看到呢