Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The above doesn't work. Reload() method does not correctly refresh the entity from the database. It performs SQL select query but does not build proxies for the navigational properties. See the example below (I use the Northwind database in SQL Server with EF 5.1):</p> <pre><code>NorthwindEntities northwindEntities = new NorthwindEntities(); Product newProduct = new Product { ProductName = "new product", Discontinued = false, CategoryID = 3 }; northwindEntities.Products.Add(newProduct); northwindEntities.SaveChanges(); // Now the product is stored in the database. Let's print its category Console.WriteLine(newProduct.Category); // prints "null" -&gt; navigational property not loaded // Find the product by primary key --&gt; returns the same object (unmodified) // Still prints "null" (due to caching and identity resolution) var productByPK = northwindEntities.Products.Find(newProduct.ProductID); Console.WriteLine(productByPK.Category); // null (due to caching) // Reloading the entity from the database doesn't help! northwindEntities.Entry&lt;Product&gt;(newProduct).Reload(); Console.WriteLine(newProduct.Category); // null (reload doesn't help) // Detach the object from the context ((IObjectContextAdapter)northwindEntities).ObjectContext.Detach(newProduct); // Now find the product by primary key (detached entities are not cached) var detachedProductByPK = northwindEntities.Products.Find(newProduct.ProductID); Console.WriteLine(detachedProductByPK.Category); // works (no caching) </code></pre> <p>I may conclude that real refresh / reload of EF entity can be done by Detach + Find:</p> <pre><code>((IObjectContextAdapter)context).ObjectContext.Detach(entity); entity = context.&lt;SomeEntitySet&gt;.Find(entity.PrimaryKey); </code></pre> <p>Nakov</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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