【问题标题】:Adding related master/child entities (one to many) in breeze在微风中添加相关的主/子实体(一对多)
【发布时间】:2013-09-25 04:05:10
【问题描述】:

我有一个小部件的主文件,并且该小部件的多个版本可以共享相同的小部件主文件。所以db上有widgetMasters和widgetVersions表。

widgetMaster ID 是一个标识整数字段,并具有一组小部件版本集。

Widgetversion 有一个外键指向它所属的 widgetMaster ID。

我的网站上有一个“创建新小部件”表单。这是在按下按钮后加载的,它使用“createEntity”为 widgetMaster 和 widgetVersion 创建空白实体。这个想法是在创建版本“001”的同时创建父“widgetMaster”。 master 只包含描述和一些类别字段。版本包含与这个版本的小部件相关的特定字段,最终可能有几十个版本。

用户填写所有字段并按“保存”。

此时我验证表单字段,如果一切正常,继续通过“datacontext.saveChanges()”保存实体。这是在我的“创建新”表单视图的视图模型中完成的。

这在创建widgetMaster时工作正常,但我认为我需要对这个过程有更多的控制......我需要在widgetVersion实体上设置外键ID由“savechanges”创建但在它尝试之前保存 widgetVersion 实体。

由于“datacontext.saveChanges()”似乎是一个一站式商店,我完全不知道如何使用刚刚保存的 widgetmaster 中新创建的 ID 来保存 widgetVersion 实体。

【问题讨论】:

  • Carl,brotha,让我们看看或至少显示代码示例
  • 公平点。现在已经想出了如何做到这一点(我认为),但遇到了我刚刚发布的另一个问题!

标签: breeze


【解决方案1】:

那好吧。我不能说这是否是 最好的 方法,但这是我完成它的方法。有关更多信息,请参阅此 stackoverflow 问题:Breeze bug? It's trying to add related entity even though only the primary entity is specified in savechanges()

我的视图模型保存方法(在允许用户填充新实体中的字段的表单条目视图上)现在是这样的:

    var save = function () {
        isSaving(true);
        //1st save widgetMaster
        return datacontext.saveChanges(new Array(newWidgetMaster())).then(function (saveResult) {
            //set version entity to have master id
            newWidgetVersion().widgetMasterID(newWidgetMaster().id());
            return datacontext.saveChanges(new Array(newWidgetVersion())).fin(complete);
        }).fail(function (e) {
            //do something with the alert
        });

        function complete() {
            isSaving(false);
        }
    };

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多