Dozer
Dozer

Reputation: 5225

How to exclude a related table when use Entity Framework

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.

Upvotes: 1

Views: 5015

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364329

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;

Upvotes: 11

Related Questions