我可以删除不在缓存中的实体吗? [英] Can I delete an entity that is not in cache?

查看:114
本文介绍了我可以删除不在缓存中的实体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中删除尚未从微风查询中检索到的记录.该实体尚未被检索,因此不在缓存中,但是我从另一个操作中知道记录的KEY.这是我尝试过的:

I want to delete a record from the DB that hasn't been retrieved from a breeze query. The entity hasn't been retrieved so it's not in the cache, but I know the KEY of the record from another operation. Here's what I've tried:

从管理者创建新实体:

manager.createEntity(entityNames.book);

设置ID:

bookToDelete().bookID(1);  // bookToDelete is a ko observable from step 1

更新状态:

bookToDelete().entityAspect.setDeleted(); 

当我保存更改时,此事务不包含在JSON中

When I save changes, this transaction is not included in the JSON

推荐答案

您几乎拥有了它.在已添加"实体上调用 entityAspect.setDeleted 会将其直接移至已分离"状态,这实际上会将其从EntityManager中删除,因此无法保存.这是故意的.它处理您创建实体然后删除它的情况.在这种情况下,没有要保存的实体.

You almost have it. Calling entityAspect.setDeleted on an 'Added' entity moves it directly to a 'Detached' state, which in effect removes it from the EntityManager, and hence it cannot be saved. This is deliberate. It handles the case where you create an entity and later delete it. In this case, there is no entity to save.

因此,在您的情况下,必须在调用 entityAspect.setDeleted 之前将实体设置为已修改"或不变".您可以通过在调用 entityAspect.setDeleted 之前先调用 entityAspect.setUnchanged entityAspect.setModified 来做到这一点,也可以调用 entityAspect.setUnifieded

So, in your case, you have to make the entity either 'Modified' or 'Unchanged' before you call entityAspect.setDeleted. You can do this by either calling entityAspect.setUnchanged or entityAspect.setModified before calling entityAspect.setDeleted or you can call entityAspect.acceptChanges.

请注意,您还必须确保克隆"实体通过验证,并且如果该实体上有并发字段,则还需要适当地设置它.

Note that you will also have to insure that the 'clone' entity passes validation and if you have a concurrency field on the entity, you will need to set this appropriately as well.

您可以通过以下步骤在标记为删除状态下创建图书实体:

You can create the book entity in the marked-for-delete state in a single step as shown:

var book = manager.createEntity(entityNames.book,
            { BookID: 1 },                  // use initializer to set the key
              breeze.EntityState.Deleted);  // creates the entity in the Deleted state

请确保使用实体通过服务器上的验证和乐观并发检查所需的所有其他属性对其进行初始化.

Be sure to initialize it with all other properties that are necessary for the entity to pass validation and optimistic concurrency checks on the server.

如果没有这些检查,没问题.如果确实进行了此类检查,则不确定在不查询服务器的情况下如何获取这些值.

No problem if you don't have these checks. Not sure how you'd get those values without querying the server if you did have such checks.

这篇关于我可以删除不在缓存中的实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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