使用实体框架时如何排除相关表 [英] How to exclude a related table when use Entity Framework

查看:57
本文介绍了使用实体框架时如何排除相关表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  db.UploadFileSet.Where(f => f.Article.ID == id).ToList(); 

ef会自动加载文章。但是我不需要它!
如何停止?



我知道我可以这样写:

 选择新的XXX {Id = xxx,Name = xxx}; 

但这很麻烦。

解决方案

所提到的查询将不会加载相关文章。当您第一次访问已加载文件集中的 Article 属性(包括调试器访问)时,将加载这些文章。如果要确保 Article 永远不会延迟加载,则必须关闭上下文的延迟加载-您可以在代码中通过在上下文配置中设置属性来实现: / p>


  • ObjectContext API: db.ContextOptions.LazyLoadingEnabled = false;

  • DbContext API: db.Configuration.LazyLoadingEnabled = false;


db.UploadFileSet.Where(f => f.Article.ID == id).ToList();

the ef will load the Article automatically.But I don't need it! How can I stop it?

I know I can write like this:

Select new XXX{Id = xxx ,Name = xxx};

But this is very troublesome.

解决方案

The mentioned query will not load related articles. Those articles are loaded when you first access Article property in your loaded file sets (this includes access by debugger). If you want to ensure that Article is never lazy loaded you have to turn off lazy loading on your context - you can do that in your code by setting property in context configuration:

  • ObjectContext API: db.ContextOptions.LazyLoadingEnabled = false;
  • DbContext API: db.Configuration.LazyLoadingEnabled = false;

这篇关于使用实体框架时如何排除相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆