在Appstart上hasChanges()== true

时间:2012-11-26 23:01:14

标签: breeze

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

不知何故,一个实体有EntityState"已添加"从服务器第一次查询时。 我在DBContextInitializer中创建了3个用户。每次我启动应用程序时都会发生这种情况它将处于State" Unchanged"如果我拨打Manager.rejectChanges(),但我必须手动执行此操作。

如果我不这样做,并保存另一个实体(可能是Team / Order / OrderDetail),则特定用户将被保存为其原始实体的副本,其中包含唯一的键,但名称等相同的值。如果我可能忘记了某些内容,我会检查我的代码十几次,但我不会在任何地方调用此用户实体的任何状态更改。

这是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但是在该数组之后不久就出现了State"添加":

"_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 个答案:

答案 0 :(得分: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);
        }
    }

答案 1 :(得分:1)

好的,这里有一个错误,服务器上的某些实体图在序列化到客户端时导致虚假的entityState更改。这应该在breeze网站上或通过nuget在v0.74.2中修复。如果这可以纠正您的问题,请告诉我们。

答案 2 :(得分:0)

想要帮助,我可以理解你被困住了。不幸的是,我提供的内容无法取得很大进展。让我们再试一次更简单的问题陈述和例子。

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

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

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

var em = new breeze.EntityManager('api/your-service');
var q = breeze.EntityQuery.from('Users')
        .using(em).execute()
        .then(querySucceeded)
        .fail(queryFailed);

function querySucceeded(data) {
    var users = data.results;
    var changes = em.getChanges();
    var msg = "User count = " + users.length +
              "; pending changes = " + changes.length;
    alert(msg);

function queryFailed(error) {
   alert('Query failed: '+error.message);
}

如果警告框确认您有3个用户且非零待定更改......您有什么东西可以让我们陷入困境。

答案 3 :(得分:0)

我一直在v 0.73.6上测试它,并确保在内部调用“updateRelatedEntityInCollection”方法。

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

我尝试使用查询“expands”,“loadNavigationProperty”,并更改了QueryOptions.MergeStrategy all而无法重现这一点;所以任何额外的提示都会非常有用。