When I have a parent Entity hold a list of other entity (one to many relationship), I modify this list then call function to save the parent Entity. With the entities has removed from this list, is that the framework will delete them from database? And also the new entity has added to the list will be added to database? thank for your help!
Assuming you have one to many relationship between Parent and Child, i. e., Parent has ChildList and Child has Parent. Looking at the cases. If Parent is in the entity context and you add an instance of Child to ChildList of Parent, and save the context, then Child will be added to the database.
Parent parent = new Parent() { Name = "parent1" };
provider.AddToParentSet(parent)
parent.ChildList.Add(new Child() { Name = "child1" });
parent.ChildList.Add(new Child() { Name = "child2" });
parent.ChildList.Add(new Child() { Name = "child3" });
provider.SaveChanges();
If you remove one of the Child from the ChildList of Parent, and save the context, then you will get an exception because of the foreign key constraint.
Parent parent = provider.ParentSet.FirstOrDefault();
parent.ChildList.Remove(parent.ChildList.FirstOrDefault());
provider.SaveChanges();
If you delete one of the Childs that belong to ChildList of Parent from the context and save the context, it will be successful, it will be removed from database.
provider.DeleteObject(parent.ChildList.FirstOrDefault());
provider.SaveChanges();
Above situations are valid for default configuration of an entity model. Entity Framework also provides many options, you can also decide how your entity context behaves. You just need to try these and such situations on your own. It will be better I think.