Note that there are some explanatory texts on larger screens.

plurals
  1. POAgain: EF 5 code first eager vs. lazy loading
    primarykey
    data
    text
    <p>There are a lot of questions about eager/lazy loading, but still there is one point that is not clear to me. I know the difference quite well, understand the "virtual" keyword and how to prevent lazy loading.</p> <p>I have a very simple model: Store, Product, ProductCategory. A store can have many products and categories, a product must belong to a store and must have a category. To make things easier I have declared a "Products" property in ProductCategory so I can get all products belonging to one category on demand. Here are my classes:</p> <pre><code>public class Store { public Store() { this.Product = new HashSet&lt;Product&gt;(); this.ProductCategory = new HashSet&lt;ProductCategory&gt;(); } [Key] public int Id { get; set; } [Required()] public string Name { get; set; } public ICollection&lt;Product&gt; Product { get; set; } public ICollection&lt;ProductCategory&gt; ProductCategory { get; set; } } public class Product { [Key] public int Id { get; set; } public int StoreId { get; set; } [Required()] public Store Store { get; set; } [Required()] [MaxLength(50)] public string Name { get; set; } public int ProductCategoryId { get; set; } [Required()] public ProductCategory ProductCategory { get; set; } } public class ProductCategory { public ProductCategory() { this.Product = new HashSet&lt;Product&gt;(); } [Key] public int Id { get; set; } public int StoreId { get; set; } [Required()] public Store Store { get; set; } public string Name { get; set; } public ICollection&lt;Product&gt; Product { get; set; } } </code></pre> <p>In my context I set Configuration.LazyLoadingEnabled = false. What is not clear is the result of the following query:</p> <pre><code>var product = context.Products.Include(p =&gt; p.ProductCategory).Single(p =&gt; p.Id == 1); </code></pre> <p>The result is: one product (expected), one category attached to product (expected), but also one product attached to category (not expected). As far as I understand how EF 5 works it first loads the product, then the category, and then attaches "automatically" the already loaded product(s) to the category entity even though I have not told EF to do this (no "Include(pc => pc.Product)"). If this is the right understanding, how can I prevent attaching the product(s) to the category? All I want is the product and its category, NOT the category with its list of products.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload