How to attach graph with added child in EF Core ?
private static void AttachGraphWithExistingParentNewChild()
{
Class class;
using (var context = new SchoolContext())
{
class = context.Classes.FirstOrDefault(s => s.Name.Contains("AA"));
}
class.Students.Add(new Student{Name= "Youssef" });
using (var context = new SchoolContext())
{
context.ChangeTracker.TrackGraph(class, e=>e.Entry.State = EntityState.Added);
}
}
This method will add the parent and the child and this 's not true ,i want to add just the child and let the parent Unchanged .
Couldn't you check e.Entry.IsKeySet? e.Entry.State = e.Entry.IsKeySet ? EntityState.Unchanged : EntityState.Added (Given that the parent will have an Id set but not the child)