entity framework - Why is cascade delete not on by default for this relationship? -


i have 1 many relationship between labellineitem , despatchpart.
can't understand why cascade delete off relationship.

there no relationship defined in context using fluent api. there no labellineitems navigation collection in despatchpart, there no reference labellineitem.

public class labellineitem {     public int id { get; set; }     public int despatchpartid { get; set; }     public int labelconfigid { get; set; }     public string content { get; set; }      // navigation     public virtual labelconfig labelconfig { get; set; }     public virtual despatchpart despatchpart { get; set; } }  public class despatchpart  {     public int id { get; set; }     public int despatchid { get; set; }      // navigation     public virtual despatch despatch { get; set; }      //... } 

it's understanding one-to-many relationships default cascade delete on. demonstrated in code sample above.

whereas zero-or-one-to-many relationships default cascade delete off case if either: - despatchpartid declared int?, - fluent api declared relationship optional i.e. despatchpart.hasmany(p => p.labellineitems).withoptional(i => i.despatchpart).

but neither of these case why i'm confused.

fyi - i'm cascade off, because when tested cascade delete removing despatch part record (in sqlmanagementstudio), received attempted fk violation in lablelineitem table tried remove referenced despatchpart record. wouldn't have occurred if delete had cascaded labellineitem table.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -