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:
[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<Child> _children;
[Association(Name = "FK_Parent_Child", DeleteRule = "CASCADE", OtherKey = "ParentId", ThisKey="Id", Storage="_children")]
public EntitySet<Child> Children
{
get
{
return _children;
}
set
{
_children.Assign(value);
}
}
public Parent()
{
_children = new EntitySet<Child>(
item => item.Parent = this,
item => 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<Parent> _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;
}
}
}
}
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.
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 ]".
It appears the DeleteRule="Cascade" isn't being honored, but I'm not sure why.