Created
November 9, 2011 09:38
-
-
Save kristofclaes/1350972 to your computer and use it in GitHub Desktop.
Removing a child entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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