Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
Created November 9, 2011 09:38
Show Gist options
  • Save kristofclaes/1350972 to your computer and use it in GitHub Desktop.
Save kristofclaes/1350972 to your computer and use it in GitHub Desktop.
Removing a child entity
var context = new MyContext();
var parent = context.Parents.Include("Children").SingleOrDefault(x => x.ParentId == 1);
var child = parent.Children.First();
var parent.Childred.Remove(child); // Is this step necessary?
var context.Childred.Remove(child); // Or is this alone sufficient?
context.SaveChanges();
public class Parent
{
public int ParentId { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public in ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
public MyContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment