부모 행이 삭제 될 때 Db의 모든 자식을 삭제하는 두 열 사이의 FK 관계를 설정하려고합니다. 내 정의는 다음과 같습니다.Mango SQL CE : DeleteRule = "Cascade"가 작동하지 않습니다.
[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;
}
}
}
}
FK 구현이 제대로 작동하는 것 같습니다. Db에 상위 행을 추가하면 하위 행이 자동으로 추가됩니다. 부모 행을 적절하게 선택하면 Children 속성이 모든 관련 하위 항목으로 채워집니다.
또한 데이터베이스의 부모 행을 삭제하고 관련 삭제 된 모든 하위 항목도 삭제할 수 있기를 바랍니다. 이 설정을 사용하면 부모를 삭제할 때 "이 키에 대한 참조가 여전히 존재하기 때문에 기본 키 값을 삭제할 수 없습니다. [외래 키 제약 이름 = FK_Child_Parent]"오류가 발생합니다.
DeleteRule = "Cascade"가 적용되지 않는 것으로 보이지만 이유가 확실하지 않습니다.
에뮬레이터에서 데이터베이스 파일을 추출하고 제약 조건을 스크립팅하여 CASCADE 규칙이 만들어 졌는지 확인하십시오. – ErikEJ
비슷한 문제가 발생했습니다. 로그 수신기를 DataContext.Log에 연결 한 후 ON CASCADE DELETE 제약 조건이 생성되지 않은 것을 확인했습니다. 속성 속성의 다른 조합으로 재생 한 후, 나는 포기했습니다. Windows Phone 용 Linq2SQL과 함께 DeleteRule = "CASCADE"는 DB 내부에서 제약 조건을 생성하지 않습니다! :/ – rObiwahn