Can EF automatically delete data that is orphaned, where the parent is not deleted?
c#, entity-framework, entity-framework-5
Solution
It is actually supported but only when you use Identifying relation. It works with code first as well. You just need to define complex key for your `ChildObject` containing both `Id` and `ParentObjectId`:
modelBuilder.Entity<ChildObject>()
.HasKey(c => new {c.Id, c.ParentObjectId});
Because defining such key will remove default convention for auto incremented Id you must redefine it manually:
modelBuilder.Entity<ChildObject>()
.Property(c => c.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Now calling to parentObject.ChildObjects.Clear() deletes dependent objects.
Btw. your relation mapping should use `WithRequired` to follow your real classes because if FK is not nullable, it is not optional:
modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects)
.WithRequired()
.HasForeignKey(c => c.ParentObjectId)
.WillCascadeOnDelete();
Problem
For an application using Code First EF 5 beta I have: ``` public class ParentObject { public int Id {get; set;} public virtual List<ChildObject> ChildObjects {get; set;} //Other members } ``` and ``` public class ChildObject { public int Id {get; set;} public int ParentObjectId {get; set;} //Other members } ``` The relevant CRUD operations are performed by repositories, where necessary. In ``` OnModelCreating(DbModelBuilder modelBuilder) ``` I have set them up: ``` modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects) .WithOptional() .HasForeignKey(c => c.ParentObjectId) .WillCascadeOnDelete(); ``` So if a `ParentObject` is deleted, its ChildObjects are too. However, if I run: ``` parentObject.ChildObjects.Clear(); _parentObjectRepository.SaveChanges(); //this repository uses the context ``` I get the exception: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. This makes sense as the definition of the entities includes the foreign key constraint which is being broken. Can I configure the entity to "clear itself up" when it gets orphaned or must I manually remove these `ChildObject`s from the context (in this case using a ChildObjectRepository).