Mango SQL CE: DeleteRule = "Cascade" not working

I am trying to set up an FK relationship between two columns that will remove all children in the Db when the parent row is removed. My definitions look like this:

    [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, it looks like the FK implementation seems to work. Adding a parent row to Db will automatically add child rows; selecting the parent row correctly populates the Children property of all children.

I would also like to be able to delete the parent row in the database and delete it, also delete all associated children. With this setup, when I remove the parent, I get the error "The primary key value could not be removed because references to that key still exist. [Foreign constraint name name = FK_Child_Parent]".

It looks like DeleteRule = "Cascade" is not being enforced, but I'm not sure why.

+3


source to share


1 answer


I know this is very late, but I had the same problem and this was the first post I found. All I want to say is that everything works.

You probably shouldn't use the name of the rule. And set the DeleteRule to the parent object.

Here is my working code.

Parent object field.



    private EntitySet<ExerciseDataContext> _exercises = new EntitySet<ExerciseDataContext>();

    [Association(Name = Constants.ForeignKeysNames.KF_GROUP_EXERCISE, Storage = "_exercises", OtherKey = "GroupID", ThisKey = "ID", DeleteRule = "Cascade")]
    public ICollection<ExerciseDataContext> Exercises
    {
        get { return _exercises; }
        set { _exercises.Assign(value); }
    }

      

And the field of the child object.

private EntityRef<GroupDataContext> _group = new EntityRef<GroupDataContext>();

    [Association(Name = Constants.ForeignKeysNames.KF_GROUP_EXERCISE, IsForeignKey = true, Storage = "_group", ThisKey = "GroupID")]
    public GroupDataContext Group
    {
        get { return _group.Entity; }
        set { _group.Entity = value; }
    }

      

Hope this helps someone.

+1


source







All Articles