How to attach graph with existing parent and new child in EF Core

asp.net c# disconnected-environment entity-framework entity-framework-core

Question

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 .

1
1
6/19/2016 10:00:27 PM

Popular Answer

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)

0
8/31/2016 5:17:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow