【问题标题】:Find id for specific entity after savechanges保存更改后查找特定实体的 ID
【发布时间】:2014-01-22 17:21:11
【问题描述】:

我有一种情况,我在单个 SaveChanges 期间保存了多种类型的实体。在某些情况下,此保存将包括我的“目标实体”,在某些情况下不包括在内。在保存确实包含“目标实体”的情况下,我需要能够使用 saveChanges() saveResult 捕获从服务器返回的实体 id。

我一直在试图弄清楚如何使用 Breeze EntityType 来查看我的“目标实体”是否在 saveResult 中,但我在下面的方法中一直未定义。显然我不明白如何使用此功能?

function trapTargetEntityId(saveResult) {
        saveResult.entities.forEach(function(entity) {
            if (entity.EntityType === 'targetEntity') {
                targetEntitId = entity.id;
            }
            return;
        });
     }

【问题讨论】:

    标签: breeze hottowel


    【解决方案1】:

    不确定我是否理解。但是如果你在保存后按键查找特定实体

    // make sure that targetType is NOT null
    var targetType = myEntityManager.metadataStore.getEntityType("Customer");
    var targetId = 23452; // arbitrary id
    
    function trapTargetEntityId(saveResult) {
    
        saveResult.entities.forEach(function(entity) {
            // assumes that you have a data property called 'id' on the 'Customer' entityType
            if (entity.entityType === targetType && entity.id === targetId) {
               targetEntity = entity;
               // do something with 'targetEntity'
            }
    
        });
     }
    

    ...小心你的外壳 - 在你的例子中它应该是'entity.entityType'而不是'entity.EntityType'

    【讨论】:

    • 玩了一会儿之后,我实际上发现我只需要从元数据存储中获取实体类型,并且实际上是回到这里来回答我自己的问题。花了我一点时间,但还是感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2021-12-11
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多