Retrieving data using .Includes and .ThenIncludes makes perfect sense and works, great! My problem then is updating the 3rd level. This is happening in a disconnected scenario and my code below has been simplified for the example. If you can retrieve data n levels deep, surely you can then update data n levels deep, right?
public class Person
{
public int id { get; set; }
public int? Address { get; set; }
public virtual Address AddressNavigation { get; set; }
}
public class Address
{
public int id { get; set; }
public string Street { get; set; }
public string Street2 { get; set; }
public int? City { get; set; }
public int? State { get; set; }
public string Zip { get; set; }
public virtual City CityNavigation { get; set; }
public virtual State StateNavigation { get; set; }
}
public class City
{
public int id { get; set; }
public string Name { get; set; }
}
public class State
{
public int id { get; set; }
public string Name { get; set; }
}
Now, if I want to update the City or State, it would seem that I could do the following:
person.AddressNavigation.CityNavigation.id = 2;
context.Attach(person);
context.Entry(person).State = EntityState.Modified;
context.SaveChanges();
What is happening, though, is that once .Attach is called, the previous value for person.AddressNavigation.CityNavigation.id is changed back to the original value, resulting in no changes being saved to the database. Why is this happening? I'm currently using EF Core 5.0.5