使用.Includes和.ThenIncludes检索数据非常有意义,而且非常有效,非常棒!我的问题是更新第三级。这是在断开连接的情况下发生的,我下面的代码已经针对示例进行了简化。如果你能检索到n层深的数据,那么你肯定也能更新n层深的数据,对吧?
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; }
}现在,如果我想要更新City或State,我似乎可以执行以下操作:
person.AddressNavigation.CityNavigation.id = 2;
context.Attach(person);
context.Entry(person).State = EntityState.Modified;
context.SaveChanges();但是,一旦调用了.Attach,person.AddressNavigation.CityNavigation.id的先前值就会更改回原始值,从而不会将任何更改保存到数据库中。为什么会发生这种情况?我目前使用的是EF Core 5.0.5
发布于 2021-06-28 13:47:29
你必须手动更新它,ef core不能自动检测到更改。请参阅this answer
https://stackoverflow.com/questions/68158067
复制相似问题