38

I have a problem with entity association refresh. When I get an entity with like this:

MyContext context = new MyContext();

Person myPerson = context.PersonSet.FirstOrDefault();
String myPersonName = myPerson.Name;
Address myPersonAddress = myPerson.Address;

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:

context.Entry(myPerson).Reload();

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.

How Can I do to force the reload of Address association (and all other association in Person class) ?

EDIT:

In the same case, a person can have more than one address.

MyContext context = new MyContext();

Person myPerson = context.PersonSet.FirstOrDefault();
String myPersonName = myPerson.Name;
List<Address> myPersonAddresses = myPerson.Addresses;

In this case, it is not a Reference:

context.Entry(myPerson).Reference(p => p.Address).Load();
// Address will be populated with only the new address
// this isn't required because I use lazy loading

but a Collection:

context.Entry(myPerson).Collection(p => p.Addresses).Load();
// Address will be populated with old value and new value

I need to use this to work:

context.Entry(myPerson).Collection(p => p.Addresses).CurrentValue.Clear();
context.Entry(myPerson).Collection(p => p.Addresses).Load();

But it doesn't seem to be a good solution to do this for all my navigation properties!

Sam
  • 687
  • 1
  • 6
  • 14
  • Do you mean that `context.Entry(myPersonAddress).Reload()` does **not** work? – Slauma Jan 31 '12 at 17:07
  • yes it means that DOESN'T work. Because this method reload only the property (=Name) and not the association (=Address) – Sam Jan 31 '12 at 17:11
  • Ah, sorry I misunderstood. So, you don't want to reload the scalar properties of the same `Address` entity but the relationship to possibly another `Address` entity. Hm, good question... – Slauma Jan 31 '12 at 17:15
  • Does `Person` have an exposed foreign key property for the `Address` navigation property? – Slauma Jan 31 '12 at 17:22
  • How can I expose a foreign key property ? What do you mean ? I have a navigation property in my .csdl – Sam Jan 31 '12 at 17:33
  • A property which represents the foreign key to `Address`, something like `public int AddressId { get; set; }` in your `Person` class. – Slauma Jan 31 '12 at 17:36
  • :o Sorry, I misunderstood. Yes, of course, I have a public property to access it. – Sam Jan 31 '12 at 17:43

4 Answers4

33

If you don't use lazy loading, you have the load the new Address explicitly (as you had to load it explicitly (with Include, for example), when you loaded the Person initially):

context.Entry(myPerson).Reload();
// If the person refers to another Address in the DB
// myPerson.Address will be null now

if (myPerson.Address == null)
    context.Entry(myPerson).Reference(p => p.Address).Load();
    // myPerson.Address will be populated with the new Address now

If you use lazy loading, you don't need the second code block. Nonetheless, you get a new query to the database as soon as you access properties of the new myPerson.Address (like you have a new query in the second code block above) because the first line will mark the navigation property as not loaded if the person refers to a new address in the DB.

This behaviour doesn't depend on whether you have exposed the foreign key in the model class or not.

There doesn't seem to be a way to call some single magic Reload method which would reload and update the whole object graph in one call (similar like there is no single Include to eager load a complete object graph).

Patman
  • 41
  • 8
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • I'm agree, there doesn't seem to be a way to call a single method reload. Unfortunately, you doesn't have only Reference in a class. You can have some collection too! (I edited my question in order cover all case) – Sam Feb 01 '12 at 09:11
  • 3
    @Sam: I see. But I think the logic is the same for collections (replace `Reference` by `Collection`). Unfortunately you have to do it separately for every navigation property (like you had to use `Include` for every navigation property for eager loading). – Slauma Feb 01 '12 at 13:59
  • @Slauma `dbContext.Entry(test).Collection(x => x.TestChildren).Load();` did not reload a Collection for Entity Framework Core. Please have a look if you have time: https://stackoverflow.com/q/65723105/3850405 – Ogglas Jan 14 '21 at 18:07
12

If it gained one entry, only your .Load() method helped.

context.Entry(myPerson).Collection(p => p.Addresses).Load();

If p.Addresses lost one entry, it can be refreshed by

((IObjectContextAdapter)CurrentContext(context)).ObjectContext.Refresh(RefreshMode.StoreWins, p.Addresses);
user3071284
  • 6,955
  • 6
  • 43
  • 57
Satria
  • 315
  • 2
  • 9
2

You need to use Query() extension to modify your LINQ expression. Here it is an example on basis of my personcal code. In this code I reload Addresses collection with related AddressType navigation property for myPerson object and place the result into SomeList:

_DbContext.Entry<Person>(myPerson)
          .Collection(i => i.Adresses) // navigation property for Person
          .Query()
          .Include("AddressType")        // navigation property for Address
          .OrderBy(i => i.Name)
          .ThenBy(i => i.AddressType.AddressTypeName) // just an example
          .Select(i => new someClass
          {
              SoomeField1 = i.SomeField1,
              ...
          })
          .ToList()
          .ForEach(i => SomeList.Add(i)); // SomeList is a List<T>
ALT
  • 1,202
  • 11
  • 7
-3

I've solved this problem with using Detach before reading object from dbContext. This method allowed me to refresh all navigation properties of the object. I've described my scenario and details of solution here Entity Framework: Reload newly created object / Reload navigation properties