BreezeJS:服务器添加的对象在保存更改后显示为在客户端中添加的对象 [英] BreezeJS: Server added object showing as added in client after save changes

查看:58
本文介绍了BreezeJS:服务器添加的对象在保存更改后显示为在客户端中添加的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个轻巧的控制器,可以在客户端未提交的保存更改期间添加一个实体。

I have a breeze controller that adds an entity during the save changes that the client did not submit.

        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);
    }

返回响应后,客户端微风管理器将设置服务器的状态侧添加的对象为已添加。上面的OrganizationUser对象最终在客户端上为添加状态。然后在下一个SaveChanges中提交它。这是一个错误吗?

When the response is returned the client side breeze manager sets the state of the server side added object as 'Added'. The OrganizationUser object above ends up with an Added state on the client. It then gets submitted in the next SaveChanges. Is this a bug?

这是第一次保存的响应:

Here is the response from the first save:

{
"$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
}]

}

此处是提交内容第二次保存:

Here is the submission of the second save:

{
"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": {}

}

请注意,第二个实体是在先前保存更改的响应中返回的实体。

Notice the second entity is the entity returned in the response of the previous save changes. It's entityState is set to Added.

我有一种解决方法,但它很脆弱,因此在服务器返回一个新实体后的每种情况下,都需要写特殊的文字。保存。有没有办法将Breeze设置为从服务器返回的所有新实体,作为对saveChanges调用的响应?

I have a work-around but its fragile and would need to be written special to every circumstance where the server returns a new entity after a save. Is there a way to set Breeze to acceptChanges on all new entities returned from the server as the the response to a saveChanges call?

                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); });


推荐答案

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

We were able to reproduce the problem and it is a bug. (The entity added on server should have returned to the client as an Unchanged entity) We are working on a fix.

=== EDIT ===

===EDIT===

对于要保存的每个实体,一次调用BeforeSaveEntity方法,并且只能操作所讨论的实体。您可以在 http://www.breezejs.com/documentation/custom-efcontextprovider

The BeforeSaveEntity method is called once for each entity to be saved and should only manipulate the entity in question. You can find more about this at http://www.breezejs.com/documentation/custom-efcontextprovider.

如果要创建更多要保存在服务器中的实体,则应在BeforeSaveEntities方法中进行操作,也可以将它们添加到savemap字典以确保将其保存在数据库中。

If you want to create more entities to be saved in the server, you should do so in the BeforeSaveEntities method, where you can also add them to the saveMap dictionary to ensure they are saved in the DB.

ie

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);
}

这篇关于BreezeJS:服务器添加的对象在保存更改后显示为在客户端中添加的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆