Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:57:20+00:00 2026-05-26T02:57:20+00:00

If i have declared entity relationship in my model as virtual then there is

  • 0

If i have declared entity relationship in my model as virtual then there is no need to use the Include statement in my LINQ query, right ??-

For ex: This is my model class :

public class Brand
{
    public int BrandID { get; set; }
    public string BrandName { get; set; }
    public string BrandDesc { get; set; }
    public string BrandUrl { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

Now, for the above model class, i dont need to use the var brandsAndProduct = pe.Brands.Include("Products").Single(brand => brand.BrandID == 22); .

Instead, I can just use the simple var brandsAndProduct = pe.Brands.Where(brand => brand.BrandID == 22); and i will automatically have the related entity available when accessed.

Am I correct in my understanding ?

Also, please tell me in what situations i should prefer one over the other ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:57:21+00:00Added an answer on May 26, 2026 at 2:57 am

    You are correct but the rule is more complex to make it really work as expected. If you define your navigation property virtual EF will at runtime create a new class (dynamic proxy) derived from your Brand class and use it instead. This new dynamically created class contains logic to load navigation property when accessed for the first time. This feature is called lazy loading (or better transparent lazy loading).

    What rules must be meet to make this work:

    • All navigation properties in class must be virtual
    • Dynamic proxy creation must not be disabled (context.Configuration.ProxyCreationEnabled). It is enabled by default.
    • Lazy loading must not be disabled (context.Configuration.LazyLoadingEnabled). It is enabled by default.
    • Entity must be attached (default if you load entity from the database) to context and context must not be disposed = lazy loading works only within scope of living context used to load it from database (or where proxied entity was attached)

    The opposite of lazy loading is called eager loading and that is what Include does. If you use Include your navigation property is loaded together with main entity.

    Usage of lazy loading and eager loading depends on your needs and also on performance. Include loads all data in single database query but it can result in huge data set when using a lot of includes or loading a lot of entities. If you are sure that you will need Brand and all Products for processing you should use eager loading.

    Lazy loading is in turn used if you are not sure which navigation property you will need. For example if you load 100 brands but you will need to access only products from one brand it is not needed to load products for all brands in initial query. The disadvantage of the lazy loading is separate query (database roundtrip) for each navigation property => if you load 100 brands without include and you will access Products property in each Brand instance your code will generate another 100 queries to populate these navigation properties = eager loading would use just singe query but lazy loading used 101 queries (it is called N + 1 problem).

    In more complex scenarios you can find that neither of these strategies perform as you need and you can use either third strategy called explicit loading or separate queries to load brands and than products for all brands you need.

    Explicit loading has similar disadvantages as lazy loading but you must trigger it manually:

    context.Entry(brand).Collection(b => b.Products).Load();
    

    The main advantages for explicit loading is ability to filter relation. You can use Query() before Load() and use any filtering or even eager loading of nested relations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
Does a OneToOne entity need to have a ManyToOne relationship to the parent entity
I have declared a DataTemplate in Window.Resources; I don't intend to use it inside
I have to map the interface as entity to use different implementations in collections.
I have an entity which is declared roughly like: @Entity @Table(name = myUserTable) public
I have written a Linq to CRM query using CRM 2011 RC (v5) LINQ-to-CRM
I have declared an enum in my server code, and would like my client
I have declared an array in the form: var refs = { 'EE810': Presence
I have declared one variable in xslt, assigned some value to it but while
I have declared: queue<int, list<int> > Q After a series of calls: Q.push(37); Q.pop();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.