gpt4 book ai didi

breeze - 删除包含子实体的实体时 Breeze 出错

转载 作者:行者123 更新时间:2023-12-01 12:45:42 28 4
gpt4 key购买 nike

我使用 Entity Framework Code First + Durandal + Breeze 开发一个项目。

我有这些实体模型:

public class Packing
{
[Key]
public int Id { get; set; }
public string PackingDescription { get; set; }
...
public virtual List<Isotope> Isotopes { get; set; }
public virtual List<PhysicalForm> PhysicalForms { get; set; }
public virtual List<ChemicalForm> ChemicalForms { get; set; }
}

public class Isotope
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
...
public int PackingId { get; set; }
public virtual Packing Packing { get; set; }
}

public class ChemicalForm
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
...
public int PackingId { get; set; }
public virtual Packing Packing { get; set; }
}

public class PhysicalForm
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
...
public int PackingId { get; set; }
public virtual Packing Packing { get; set; }
}

当我运行我的项目时,我的 Entity Framework Code First 数据库被创建。

首先,我直接在我的数据库中“手动”测试级联删除。当我有一个包含多个同位素的 Packing 并删除该 Packing 时,所有级联的同位素都将被删除。这对我来说没问题。

现在在我使用 breeze 的项目运行时,当我尝试相同的场景时:删除一个 Packing 元素,如下所示:

var deletePackings = function (packingsObservable) {

// Input: packingsObservable: an observable filled with a list of packings to delete
// Output: none

for (var i = 0; i < packingsObservable().length; i++) {
packingsObservable()[i].entityAspect.setDeleted();
};
return manager.saveChanges();
};

我收到以下错误:

The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Isotopes_dbo.Packings_PackingId\". The conflict occurred in database \"TRANSPORTBOEKDB\", table \"dbo.Packings\", column 'Id'.\r\nThe statement has been terminated."}

现在我读了另一个SO post那个

Breeze does not yet support client side 'cascaded' deletes (we are considering this one), you will need to iterate over any client side orders that are already loaded and 'detach' them.

这就是我的应用程序出现错误的原因吗?

我是否必须遍历任何已加载的子实体并“分离”它们?


更新

通过轻而易举地通过代码手动分离任何子实体可以解决问题,但这很痛苦:

var deletePackings = function (packingsObservable) {

// Input: packingsObservable: an observable filled with a list of packings to delete
// Output: none
// Remark: we loop from end to begin of the observable!

var entity;

// Since Breeze does not yet support client side 'cascaded' deletes (we are considering this one),
// you will need to iterate over any child entity that are already loaded and 'detach' them.

for (var i = packingsObservable().length - 1; i >= 0; i--) {

// Detach any child entities of type isotope
for (var j = packingsObservable()[i].isotopes().length - 1; j >= 0; j--) {
entity = packingsObservable()[i].isotopes()[j];
manager.detachEntity(entity);
}

// Detach any child entities of type chemicalForm
for (var j = packingsObservable()[i].chemicalForms().length - 1; j >= 0; j--) {
entity = packingsObservable()[i].chemicalForms()[j];
manager.detachEntity(entity);
}

// Detach any child entities of type physicalForm
for (var j = packingsObservable()[i].physicalForms().length - 1; j >= 0; j--) {
entity = packingsObservable()[i].physicalForms()[j];
manager.detachEntity(entity);
}

packingsObservable()[i].entityAspect.setDeleted();
};


return manager.saveChanges();
};

没有更好的解决方案?

最佳答案

如果需要,在 SQL 中执行级联删除并在客户端忘记,之后只需刷新数据。

或者您可以为聚合实体添加新的 IsActive 列,这是我的首选方法,我害怕数据库删除:)

关于breeze - 删除包含子实体的实体时 Breeze 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16221693/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com