Note that there are some explanatory texts on larger screens.

plurals
  1. POMango SQL CE: DeleteRule="Cascade" not working
    primarykey
    data
    text
    <p>I'm trying to setup a FK relationship between two columns that will delete all children in the Db when a parent row is deleted. My definitions look like:</p> <pre><code> [Table] public class Parent { [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] public int Id { get; set; } [Column] public string Dummy { get { return "dummy"; } set { } } private EntitySet&lt;Child&gt; _children; [Association(Name = "FK_Parent_Child", DeleteRule = "CASCADE", OtherKey = "ParentId", ThisKey="Id", Storage="_children")] public EntitySet&lt;Child&gt; Children { get { return _children; } set { _children.Assign(value); } } public Parent() { _children = new EntitySet&lt;Child&gt;( item =&gt; item.Parent = this, item =&gt; item.Parent = null); } } [Table] public class Child { [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] public int Id { get; set; } [Column] public int? ParentId { get; set; } private EntityRef&lt;Parent&gt; _parent; [Association(Name="FK_Child_Parent", ThisKey = "ParentId", Storage = "_parent", OtherKey = "Id", IsForeignKey = true, DeleteRule="CASCADE")] public Parent Parent { get { return _parent.Entity; } set { var previousValue = _parent.Entity; if (previousValue != value || !this._parent.HasLoadedOrAssignedValue) { if (previousValue != null) _parent.Entity = null; _parent.Entity = value; if (value != null) ParentId = value.Id; else ParentId = null; } } } } </code></pre> <p>From what I can tell this seems implementation of FKs seems to work. Adding a parent row to the Db will automatically add child rows; selecting a parent row properly fills in the Children property with all related children. </p> <p>I would also like to be able to delete a parent row in the database and have that delete also remove all related children. With this setup, when I delete a parent I get the error "The primary key value cannot be deleted because references to this key still exist. [ Foreign key constraint name = FK_Child_Parent ]".</p> <p>It appears the DeleteRule="Cascade" isn't being honored, but I'm not sure why.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload