gpt4 book ai didi

Breeze JS : Server added object showing as added in client after save changes

转载 作者:行者123 更新时间:2023-12-02 22:01:48 26 4
gpt4 key购买 nike

我有一个 breeze Controller ,它在客户端未提交的保存更改期间添加一个实体。

        protected override bool BeforeSaveEntity(EntityInfo entityInfo)
{
if (entityInfo.Entity.GetType() == typeof(User))
{
if (entityInfo.EntityState == EntityState.Added)
{
User user = entityInfo.Entity as User;
OrganizationUser orgUser = new OrganizationUser()
{
Enabled = true,
OrganizationId = User.OrgId,
User = user
};
user.OrganizationUsers.Add(orgUser);
}
return true;
}
throw new InvalidOperationException("You can not use this service to modify an entity of type: " + entityInfo.Entity.GetType().Name);
}

当返回响应时,客户端 Breeze 管理器将服务器端添加对象的状态设置为“已添加”。上面的 OrganizationUser 对象在客户端以 Added 状态结束。然后在下一个 SaveChanges 中提交。这是错误吗?

这是第一次保存的响应:

{
"$id": "1",
"$type": "Breeze.WebApi.SaveResult, Breeze.WebApi",
"Entities": [{
"$id": "2",
"$type": "LeonardoMD.Server.Api.Security.Admin.User, LeonardoMD.Server",
"Id": 9176,
"Email": "SearchProviderA@leonardoMD.com",
"FirstName": "SearchProviderA",
"LastName": "SearchProviderA",
"OrganizationUsers": [{
"$id": "3",
"$type": "LeonardoMD.Server.Api.Security.Admin.OrganizationUser, LeonardoMD.Server",
"UserId": 9176,
"User": { "$ref": "2" },
"OrganizationId": 421,
"Enabled": true
}]
}],
"KeyMappings": [{
"$id": "4",
"$type": "Breeze.WebApi.KeyMapping, Breeze.WebApi",
"EntityTypeName": "LeonardoMD.Server.Api.Security.Admin.User",
"TempValue": -1,
"RealValue": 9176
}]

这是第二次保存的提交:

{
"entities": [{
"Id": -2,
"CreateDate": null,
"CreateUserId": null,
"ModifyDate": null,
"ModifyUserId": null,
"Email": "SearchProviderB@leonardoMD.com",
"FirstName": "SearchProviderB",
"LastName": "SearchProviderB",
"BirthDate": null,
"GenderId": null,
"AddressLine1": null,
"AddressLine2": null,
"City": null,
"State": null,
"StateId": null,
"PostalCode": null,
"CountryId": null,
"DayPhone": null,
"DayPhoneExtension": null,
"Password": null,
"SecretQuestion": null,
"SecretAnswer": null,
"Enabled": null,
"AcceptTermsDate": null,
"entityAspect": {
"entityTypeName": "User:#LeonardoMD.Server.Api.Security.Admin",
"defaultResourceName": "Users",
"entityState": "Added",
"originalValuesMap": {},
"autoGeneratedKey": {
"propertyName": "Id",
"autoGeneratedKeyType": "Identity"
}
}
},
{
"UserId": 9176,
"OrganizationId": 421,
"Enabled": true,
"entityAspect": {
"entityTypeName": "OrganizationUser:#LeonardoMD.Server.Api.Security.Admin",
"defaultResourceName": "OrganizationUsers",
"entityState": "Added",
"originalValuesMap": {},
"autoGeneratedKey": null
}
}],
"saveOptions": {}

请注意,第二个实体是在先前保存更改的响应中返回的实体。它的 entityState 设置为已添加。

我有一个解决方法,但它很脆弱,需要针对服务器在保存后返回新实体的每种情况进行特殊编写。有没有一种方法可以将 Breeze 设置为接受服务器返回的所有新实体上的更改作为对 saveChanges 调用的响应?

                manager.saveChanges()
.then(function (saveResult) {
$.each(saveResult.entities, function (i, entity) {
if (entity.organizationUsers && entity.organizationUsers().length > 0)
$.each(entity.organizationUsers(), function (index, orgUser) {
orgUser.entityAspect.acceptChanges();
});
entity.entityAspect.acceptChanges();
});
dfd.resolve();
})
.fail(function (error) { _this.handleError(context, promise, error); });

最佳答案

我们能够重现该问题,这是一个错误。 (在服务器上添加的实体应该作为未更改的实体返回给客户端)我们正在努力修复。

===编辑===

为每个要保存的实体调用一次 BeforeSaveEntity 方法,并且应该只操作相关实体。您可以在 http://www.breezejs.com/documentation/custom-efcontextprovider 找到更多相关信息。 .

如果你想创建更多的实体保存在服务器中,你应该在 BeforeSaveEntities 方法中这样做,你也可以将它们添加到 saveMap 字典中以确保它们保存在数据库中。

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {
Dictionary<Type, List<EntityInfo>> saveMapAdditions = new Dictionary<Type, List<EntityInfo>>();

var prod = this.CreateEntityInfo(product, EntityState.Added);
// add data to the entity
saveMapAdditions[typeof(Product)].Add(prod);

return base.BeforeSaveEntities(saveMap);
}

关于 Breeze JS : Server added object showing as added in client after save changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16838993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com