删除包含子实体的实体时微风错误 [英] Error with breeze when deleting an entity which contains child entities

查看:65
本文介绍了删除包含子实体的实体时微风错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework Code First + Durandal + Breeze进行项目。

I work on a project with 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; }
}

运行项目时,将创建实体框架代码优先数据库。

When I run my project my Entity Framework Code First database is created.

首先,我直接在数据库中测试手动级联删除。当我有一个包含多个同位素的填料并删除该填料时,所有级联的同位素都会被删除。

First, I test the cascade delete 'by hand' directly in my database. When I have one Packing with multiple Isotopes and I delete the Packing, all cascaded Isotopes are deleted. This is ok for me.

现在,当我尝试相同的情况时,在运行时使用微风在我的项目中运行:删除包装这样的元素:

Now at runtime in my project using breeze, when I try the same scenario: delete a Packing element like this:

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();
};

我遇到以下错误:


UPDATE语句与FOREIGN KEY约束 FK_dbo.Isotopes_dbo.Packings_PackingId冲突。数据库\ TRANSPORTBOEKDB\的表\ dbo.Packings\的列 Id中发生冲突。\r\n语句已终止。}

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."}

现在我在另一个上阅读因此,帖子


微风尚不支持客户端的级联删除(我们正在考虑这一点),您将需要遍历已加载的所有客户端订单并分离它们。

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.

所以这就是为什么我得到了我的应用程序中出现错误?

So is this the reason why I got the error in my application?

我是否必须遍历所有已加载的子实体并将它们分离?

Do I have to iterate over any child entities that are already loaded and 'detach' them?

更新

通过使用微风代码手动分离任何子实体可以达到目的,但这很痛苦:

By manually detaching any child entities by code with breeze do the trick but this is painful:

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中进行级联删除,然后忘记在客户端上登录,然后刷新数据。

Do cascade delete in SQL if needed and forget in on client, just refresh data after.

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

Or you can add new IsActive column for agreggate entity, this is my prefered approach, I am scared of DB deletes:)

这篇关于删除包含子实体的实体时微风错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆