Breeze BeforeSaveEntityonly仅允许更新已添加的实体 [英] Breeze BeforeSaveEntityonly only allows update to Added entities

查看:59
本文介绍了Breeze BeforeSaveEntityonly仅允许更新已添加的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是故意的还是错误,但是下面的使用BeforeSaveEntity的以下代码将仅修改新创建的记录的实体(EntityState =已添加),而无法修改,这是正确的吗? / p>

 受保护的覆盖布尔值BeforeSaveEntity(EntityInfo entityInfo)
{
varentity = entityInfo.Entity;
如果(实体是用户)
{
var user =实体为User;
user.ModifiedDate = DateTime.Now;
user.ModifiedBy = 1;
}
...


解决方案

此问题的根源在于,在微风服务器上,我们没有针对服务器上所做更改的任何内置更改跟踪机制。服务器实体可以是纯poco。微风客户端对任何客户端更改都具有丰富的更改跟踪功能,但是当您进入服务器时,需要自己进行管理。



发生此问题是由于我们在服务器上执行了优化,因此我们仅更新那些已更改的属性。即,以便仅对更改的列执行任何SQL更新语句。显然,这对于添加或删除或在我们更新客户端上已更新的列的情况下不是问题。但是,如果您更新服务器上未在客户端上更新的字段,那么breeze对此一无所知。



从理论上讲,我们可以对进入服务器的每个实体进行快照,然后遍历该实体上的每个字段,以确定在保存拦截期间是否进行了任何更改,但我们确实讨厌性能影响,尤其是因为这种情况很少发生。



因此,在另一个答案中提出的更新服务器端OriginalValuesMap的建议是正确的,并且将完全满足您的需要。 / p>

此外,从1.1.3版开始,您还可以设置一个附加的 EntityInfo.ForceUpdate 标志,该标志将告诉微风更新每个指定实体中的列。

希望这会有所帮助。

>

Don't know if this is intended or a bug, but the following code below using BeforeSaveEntity will only modify the entity for newly created records (EntityState = Added), and won't work for modified, is this correct?

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        var entity = entityInfo.Entity;
        if (entity is User)
        {
            var user = entity as User;
            user.ModifiedDate = DateTime.Now;
            user.ModifiedBy = 1;
        }
...

解决方案

The root of this issue is that on the breeze server we don’t have any built in change tracking mechanism for changes made on the server. Server entities can be pure poco. The breeze client has a rich change tracking capability for any client side changes but when you get to the server you need to manage this yourself.

The problem occurs because of an optimization we perform on the server so that we only update those properties that are changed. i.e. so that any SQL update statements are only made to the changed columns. Obviously this isn’t a problem for Adds or Deletes or those cases where we update a column that was already updated on the client. But if you update a field on the server that was not updated on the client then breeze doesn't know anything about it.

In theory we could snapshot each entity coming into the server and then iterate over every field on the entity to determine if any changes were made during save interception but we really hate the perf implications especially since this case will rarely occur.

So the suggestion made in another answer here to update the server side OriginalValuesMap is correct and will do exactly what you need.

In addition, as of version 1.1.3, there is an additional EntityInfo.ForceUpdate flag that you can set that will tell breeze to update every column in the specified entity. This isn't quite as performant as the suggestion above, but it is simpler, and the effects will be the same in either case.

Hope this helps.

这篇关于Breeze BeforeSaveEntityonly仅允许更新已添加的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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