Note that there are some explanatory texts on larger screens.

plurals
  1. POReload an entity and all Navigation Property Association- DbSet Entity Framework
    primarykey
    data
    text
    <p>I have a problem with entity association refresh. When I get an entity with like this:</p> <pre><code>MyContext context = new MyContext(); Person myPerson = context.PersonSet.FirstOrDefault(); String myPersonName = myPerson.Name; Address myPersonAddress = myPerson.Address; </code></pre> <p>I got an a person with an association named Address and a property named Name. If I modify manually the datas in database for example the property Name, I have to use the following code to reload my entity:</p> <pre><code>context.Entry(myPerson).Reload(); </code></pre> <p>and I have the new value for Name. But If I do the same for Address it doesn't work. I think it is because Address is an association property. I need to refresh it. </p> <p>How Can I do to force the reload of Address association (and all other association in Person class) ?</p> <p>EDIT:</p> <p>In the same case, a person can have more than one address. </p> <pre><code>MyContext context = new MyContext(); Person myPerson = context.PersonSet.FirstOrDefault(); String myPersonName = myPerson.Name; List&lt;Address&gt; myPersonAddresses = myPerson.Addresses; </code></pre> <p>In this case, it is not a Reference:</p> <pre><code>context.Entry(myPerson).Reference(p =&gt; p.Address).Load(); // Address will be populated with only the new address // this isn't required because I use lazy loading </code></pre> <p>but a Collection:</p> <pre><code>context.Entry(myPerson).Collection(p =&gt; p.Addresses).Load(); // Address will be populated with old value and new value </code></pre> <p>I need to use this to work: </p> <pre><code>context.Entry(myPerson).Collection(p =&gt; p.Addresses).CurrentValue.Clear(); context.Entry(myPerson).Collection(p =&gt; p.Addresses).Load(); </code></pre> <p>But it doesn't seem to be a good solution to do this for all my navigation properties!</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.
 

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