专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > LINQ

关于entity framework 外键有关问题(使用Linq-to-Entity)

发布时间:2011-06-24 21:55:02 文章来源:www.iduyao.cn 采编人员:星星草
关于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 为外键列

------解决方案--------------------
探讨
如果能选择是否根据外键引用在模型中生成列,请问在什么地方选择的?没看到呢
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: