1

编辑:JSON不完整,不得不削减一些

第一次从服务器查询时,某个实体以某种方式具有“已添加”的 EntityState。我在 DBContextInitializer 中创建了 3 个用户。每次我启动我的应用程序时都会发生这种情况。如果我打电话,它将处于“未更改”状态Manager.rejectChanges(),但我必须手动执行此操作。

如果我不这样做,并保存另一个实体(可能是团队/订单/订单详细信息),则特定用户将被保存为其原始实体的副本,具有唯一键但具有相同值(如名称等)。如果我可能忘记了什么,我检查了我的代码十几次,但我不会在任何地方调用此用户实体的任何状态更改。

这是Manager.getChanges(“用户”的实体类型)的打印:

[
[
    {
        "entityAspect": {
            "entityGroup": {
                "entityManager": {
                    "serviceName": "api/Breeze/",
                    "metadataStore": {
                        "namingConvention": {
                            "name": "noChange"
                        },
                        "localQueryComparisonOptions": {
                            "name": "caseInsensitiveSQL",
                            "isCaseSensitive": false,
                            "usesSql92CompliantStringComparison": true
                        },
                        "dataServices": [
                            {
                                "serviceName": "api/Breeze/",
                                "adapterName": null,
                                "hasServerMetadata": true
                            }
                        ],
                        "_resourceEntityTypeMap": {
                            "Mvls": "Mvl:#MAHN.Model",
                            "MvlOps": "MvlOP:#MAHN.Model",
                            "StatusOps": "StatusOP:#MAHN.Model",
                            "Teams": "Team:#MAHN.Model",
                            "TeamLeaders": "TeamLeaders:#MAHN.Model",
                            "Users": "User:#MAHN.Model",
                            "TeamUsers": "TeamUsers:#MAHN.Model",
                            "UserRoles": "UserRoles:#MAHN.Model",
                            "Roles": "Role:#MAHN.Model",
                            "TeamRoles": "TeamRoles:#MAHN.Model"
                        },
                        "_entityTypeResourceMap": {
                            "Mvl:#MAHN.Model": "Mvls",
                            "MvlOP:#MAHN.Model": "MvlOps",
                            "StatusOP:#MAHN.Model": "StatusOps",
                            "Team:#MAHN.Model": "Teams",
                            "TeamLeaders:#MAHN.Model": "TeamLeaders",
                            "User:#MAHN.Model": "Users",
                            "TeamUsers:#MAHN.Model": "TeamUsers",
                            "UserRoles:#MAHN.Model": "UserRoles",
                            "Role:#MAHN.Model": "Roles",
                            "TeamRoles:#MAHN.Model": "TeamRoles"
                        },
                        "_entityTypeMap": {
                          "User:#MAHN.Model": {
                                "name": "User:#MAHN.Model",
                                "shortName": "User",
                                "namespace": "MAHN.Model",
                                "defaultResourceName": "Users",
                                "dataProperties": [
                                    {
                                        "name": "UserId",
                                        "nameOnServer": "UserId",
                                        "dataType": "Int32",
                                        "isNullable": false,
                                        "defaultValue": 0,
                                        "validators": [
                                            {
                                                "validatorName": "required"
                                            },
                                            {
                                                "validatorName": "int32"
                                            }
                                        ],
                                        "isPartOfKey": true
                                    },
                                    {
                                        "name": "UserName",
                                        "nameOnServer": "UserName",
                                        "dataType": "String",
                                        "isNullable": true,
                                        "fixedLength": false,
                                        "validators": [
                                            {
                                                "validatorName": "string"
                                            }
                                        ],
                                        "isPartOfKey": false
                                    },
                                    {
                                        "name": "Nachname",
                                        "nameOnServer": "Nachname",
                                        "dataType": "String",
                                        "isNullable": true,
                                        "fixedLength": false,
                                        "validators": [
                                            {
                                                "validatorName": "string"
                                            }
                                        ],
                                        "isPartOfKey": false
                                    },
                                    {
                                        "name": "Vorname",
                                        "nameOnServer": "Vorname",
                                        "dataType": "String",
                                        "isNullable": true,
                                        "fixedLength": false,
                                        "validators": [
                                            {
                                                "validatorName": "string"
                                            }
                                        ],
                                        "isPartOfKey": false
                                    },
                                    {
                                        "name": "OasisLogin",
                                        "nameOnServer": "OasisLogin",
                                        "dataType": "String",
                                        "isNullable": true,
                                        "fixedLength": false,
                                        "validators": [
                                            {
                                                "validatorName": "string"
                                            }
                                        ],
                                        "isPartOfKey": false
                                    }
                                ],
                                "navigationProperties": [
                                    {
                                        "name": "TeamUsers",
                                        "nameOnServer": "TeamUsers",
                                        "entityTypeName": "TeamUsers:#MAHN.Model",
                                        "isScalar": false,
                                        "associationName": "TeamUsers_User",
                                        "foreignKeyNames": [],
                                        "foreignKeyNamesOnServer": [],
                                        "validators": []
                                    },
                                    {
                                        "name": "UserRoles",
                                        "nameOnServer": "UserRoles",
                                        "entityTypeName": "UserRoles:#MAHN.Model",
                                        "isScalar": false,
                                        "associationName": "UserRoles_User",
                                        "foreignKeyNames": [],
                                        "foreignKeyNamesOnServer": [],
                                        "validators": []
                                    }
                                ],
                                "autoGeneratedKeyType": "Identity",
                                "validators": []
                            },


                        "Team:#MAHN.Model": {
                            "entityType": {
                                "name": "Team:#MAHN.Model",
                                "shortName": "Team",
                                "namespace": "MAHN.Model",
                                "defaultResourceName": "Teams",
                                "autoGeneratedKeyType": "Identity"
                            },
                            "_indexMap": {
                                "1": 1,
                                "2": 0,
                                "3": 2,
                                "4": 3
                            },
                            "_entities": [
                                {
                                    "entityAspect": {
                                        "entityState": {
                                            "_$typeName": "EntityState",
                                            "name": "Unchanged"
                                        },
                                        "isBeingSaved": false,
                                        "originalValues": {},
                                        "_validationErrors": {},
                                        "validationErrorsChanged": {
                                            "name": "validationErrorsChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "propertyChanged": {
                                            "name": "propertyChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "_entityKey": {
                                            "entityType": "Team:#MAHN.Model",
                                            "values": [
                                                2
                                            ]
                                        },
                                        "wasLoaded": true
                                    }
                                },
                                {
                                    "entityAspect": {
                                        "entityState": {
                                            "_$typeName": "EntityState",
                                            "name": "Unchanged"
                                        },
                                        "isBeingSaved": false,
                                        "originalValues": {},
                                        "_validationErrors": {},
                                        "validationErrorsChanged": {
                                            "name": "validationErrorsChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "propertyChanged": {
                                            "name": "propertyChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "_entityKey": {
                                            "entityType": "Team:#MAHN.Model",
                                            "values": [
                                                1
                                            ]
                                        },
                                        "wasLoaded": true
                                    }
                                },
                                {
                                    "entityAspect": {
                                        "entityState": {
                                            "_$typeName": "EntityState",
                                            "name": "Unchanged"
                                        },
                                        "isBeingSaved": false,
                                        "originalValues": {},
                                        "_validationErrors": {},
                                        "validationErrorsChanged": {
                                            "name": "validationErrorsChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "propertyChanged": {
                                            "name": "propertyChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "_entityKey": {
                                            "entityType": "Team:#MAHN.Model",
                                            "values": [
                                                3
                                            ]
                                        },
                                        "wasLoaded": true
                                    }
                                },
                                {
                                    "entityAspect": {
                                        "entityState": {
                                            "_$typeName": "EntityState",
                                            "name": "Unchanged"
                                        },
                                        "isBeingSaved": false,
                                        "originalValues": {},
                                        "_validationErrors": {},
                                        "validationErrorsChanged": {
                                            "name": "validationErrorsChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "propertyChanged": {
                                            "name": "propertyChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "_entityKey": {
                                            "entityType": "Team:#MAHN.Model",
                                            "values": [
                                                4
                                            ]
                                        },
                                        "wasLoaded": true
                                    }
                                }
                            ],
                            "_emptyIndexes": []
                        },
                        "TeamLeaders:#MAHN.Model": {
                            "entityType": {
                                "name": "TeamLeaders:#MAHN.Model",
                                "shortName": "TeamLeaders",
                                "namespace": "MAHN.Model",
                                "defaultResourceName": "TeamLeaders",
                                "autoGeneratedKeyType": "Identity"
                            },
                            "_indexMap": {
                                "1": 0
                            },
                            "_entities": [
                                {
                                    "entityAspect": {
                                        "entityState": {
                                            "_$typeName": "EntityState",
                                            "name": "Unchanged"
                                        },
                                        "isBeingSaved": false,
                                        "originalValues": {},
                                        "_validationErrors": {},
                                        "validationErrorsChanged": {
                                            "name": "validationErrorsChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "propertyChanged": {
                                            "name": "propertyChanged_entityAspect",
                                            "_nextUnsubKey": 1
                                        },
                                        "_entityKey": {
                                            "entityType": "TeamLeaders:#MAHN.Model",
                                            "values": [
                                                1
                                            ]
                                        },
                                        "wasLoaded": true
                                    }
                                }
                            ],
                            "_emptyIndexes": []
                        }
                    },
                    "_unattachedChildrenMap": {
                        "map": {}
                    },
                    "keyGenerator": {
                        "_tempIdMap": {},
                        "nextNumber": -1,
                        "nextNumberIncrement": -1,
                        "stringPrefix": "K_"
                    },
                    "_hasChanges": true
                },
                "entityType": {
                    "name": "User:#MAHN.Model",
                    "shortName": "User",
                    "namespace": "MAHN.Model",
                    "defaultResourceName": "Users",
                    "autoGeneratedKeyType": "Identity"
                },
                "_indexMap": {
                    "1": 2,
                    "2": 0,
                    "3": 1
                },
                "_entities": [
                    {
                        "entityAspect": {
                            "entityState": {
                                "_$typeName": "EntityState",
                                "name": "Unchanged"
                            },
                            "isBeingSaved": false,
                            "originalValues": {},
                            "_validationErrors": {},
                            "validationErrorsChanged": {
                                "name": "validationErrorsChanged_entityAspect",
                                "_nextUnsubKey": 1
                            },
                            "propertyChanged": {
                                "name": "propertyChanged_entityAspect",
                                "_nextUnsubKey": 1
                            },
                            "_entityKey": {
                                "entityType": "User:#MAHN.Model",
                                "values": [
                                    2
                                ]
                            },
                            "wasLoaded": true
                        }
                    },
                    {
                        "entityAspect": {
                            "entityState": {
                                "_$typeName": "EntityState",
                                "name": "Unchanged"
                            },
                            "isBeingSaved": false,
                            "originalValues": {},
                            "_validationErrors": {},
                            "validationErrorsChanged": {
                                "name": "validationErrorsChanged_entityAspect",
                                "_nextUnsubKey": 1
                            },
                            "propertyChanged": {
                                "name": "propertyChanged_entityAspect",
                                "_nextUnsubKey": 1
                            },
                            "_entityKey": {
                                "entityType": "User:#MAHN.Model",
                                "values": [
                                    3
                                ]
                            },
                            "wasLoaded": true
                        }
                    },
                    null
                ],
                "_emptyIndexes": []
            },
            "entityState": {
                "_$typeName": "EntityState",
                "name": "Added"
            },
            "isBeingSaved": false,
            "originalValues": {},
            "_validationErrors": {},
            "validationErrorsChanged": {
                "name": "validationErrorsChanged_entityAspect",
                "_nextUnsubKey": 1
            },
            "propertyChanged": {
                "name": "propertyChanged_entityAspect",
                "_nextUnsubKey": 1
            },
            "_entityKey": {
                "entityType": "User:#MAHN.Model",
                "values": [
                    1
                ]
            },
            "wasLoaded": true
        }
    }
 ]
]

我无法从中提取很多信息,但我认为这个特殊部分很奇怪。为什么该数组中的第三个对象null但在该数组之后不久显示为“已添加”状态:

"_entities": [
                {
                    "entityAspect": {
                        "entityState": {
                            "_$typeName": "EntityState",
                            "name": "Unchanged"
                        },
                        "isBeingSaved": false,
                        "originalValues": {},
                        "_validationErrors": {},
                        "validationErrorsChanged": {
                            "name": "validationErrorsChanged_entityAspect",
                            "_nextUnsubKey": 1
                        },
                        "propertyChanged": {
                            "name": "propertyChanged_entityAspect",
                            "_nextUnsubKey": 1
                        },
                        "_entityKey": {
                            "entityType": "User:#MAHN.Model",
                            "values": [
                                2
                            ]
                        },
                        "wasLoaded": true
                    }
                },
                {
                    "entityAspect": {
                        "entityState": {
                            "_$typeName": "EntityState",
                            "name": "Unchanged"
                        },
                        "isBeingSaved": false,
                        "originalValues": {},
                        "_validationErrors": {},
                        "validationErrorsChanged": {
                            "name": "validationErrorsChanged_entityAspect",
                            "_nextUnsubKey": 1
                        },
                        "propertyChanged": {
                            "name": "propertyChanged_entityAspect",
                            "_nextUnsubKey": 1
                        },
                        "_entityKey": {
                            "entityType": "User:#MAHN.Model",
                            "values": [
                                3
                            ]
                        },
                        "wasLoaded": true
                    }
                },
                null
            ],

如果有帮助,我可以提供更多信息。

4

4 回答 4

1

我有同样的问题。我发现在我的情况下导致这种情况的是结果中包含的导航属性(很多)。我仍在对此进行调查,但当将接收到的关联对象与“主要”对象连接起来时,Breeze 中的 updateRelatedEntityInCollection() 似乎设置了“已添加”。

function updateRelatedEntityInCollection(relatedEntity, relatedEntities, targetEntity, inverseProperty) {
        if (!relatedEntity) return;
        // check if the related entity is already hooked up
        var thisEntity = relatedEntity.getProperty(inverseProperty.name);
        if (thisEntity !== targetEntity) {
            // if not - hook it up.
            relatedEntities.push(relatedEntity); // <-- THIS WILL SET "ADDED" ON targetEntity
            relatedEntity.setProperty(inverseProperty.name, targetEntity);
        }
    }
于 2012-11-27T12:35:27.473 回答
1

好的,这里有一个错误,服务器上的某些实体图在序列化到客户端时会导致虚假的 entityState 更改。这应该在微风网站上或通过 nuget 提供的 v0.74.2 中修复。请让我们知道这是否可以解决您的问题。

于 2012-11-30T02:24:42.667 回答
0

想帮忙,我很感激你被困住了。不幸的是,您提供的内容我无法取得太大进展。让我们用更简单的问题陈述和示例再试一次。

一个好的开始是查看来自对服务的简单 HTTP 调用的数据。尝试在浏览器地址栏中输入如下内容:
http://localhost:12345/api/your-service/users

您应该会看到您创建的三个用户的 JSON 数据。

现在尝试在客户端获取它们:

var em = new 微风.EntityManager('api/your-service');
var q = 微风.EntityQuery.from('用户')
        .using(em).execute()
        .then(查询成功)
        . 失败(查询失败);

函数查询成功(数据){
    var users = data.results;
    var changes = em.getChanges();
    var msg = "用户数 = " + users.length +
              "; 待定更改 = " + changes.length;
    警报(味精);

函数查询失败(错误){
   alert('查询失败:'+error.message);
}

如果警报框确认您有 3 个用户和非零未决更改……您有一些我们可以深入研究的东西。

于 2012-11-27T00:12:34.580 回答
0

我一直在 v 0.73.6 上对此进行测试,并确保内部调用了“updateRelatedEntityInCollection”方法。

到目前为止,我还无法重现 entityManager.hasChanges() 返回 true 或 EntityManager.getChanges() 作为任何查询结果返回任何实体的问题。

我尝试使用查询“expands”、“loadNavigationProperty”,并更改 QueryOptions.MergeStrategy 都无法重现此;所以任何额外的提示都会非常有帮助。

于 2012-11-28T20:59:41.287 回答