更新后但提交之前进行breezejs和实体框架验证 [英] breezejs and entity framework validation after updates, but before commit

查看:56
本文介绍了更新后但提交之前进行breezejs和实体框架验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定这是微风问题还是EF问题。我有一个SaveChanges映射,其中包含插入/删除/更新。我的业务规则验证涉及多个实体以及它们之间的关系,因此在我知道所有更改之前,我无法进行完整的验证。相同的验证逻辑在客户端上执行,但是我在服务器上重新验证。

not sure if this is a breeze question or an EF question. I have a SaveChanges map that contains inserts/deletes/updates. I have business rule validations that involve multiple entities and relationships between them, so I can't do the complete validation until I know all the changes. The same validation logic is performed on the client, but I am re-validating on the server.

我正在寻找某种方法来应用完整的sql批处理更新,但是在提交事务之前,我想进行验证以查看更改集作为一个整体是否有效。如果没有,那么我回滚。当然,验证必须在与更新相同的上下文/事务中完成,以便我可以访问更改。

What I am looking for is some way to apply the complete sql batch of updates, but before the transaction is committed I would like to do the validations to see if the change set as a whole is valid. If not, then I roll back. of course the validations would have to be done within the same context/transaction as the updates so I have access to the changes.

我已经在BeforeSaveEntities中进行了验证,但是多数民众赞成在安全检查,(该用户可以更新此记录)。那很简单明了。

I'm already doing validation in BeforeSaveEntities but thats more of a security check, (can this user update this record). Thats simple and straightforward.

包含客户记录和1个或更多地址记录和/或1个或更多联系记录的变更集的业务规则示例:如果将客户分配给然后,行业A必须将确切的1个地址编码为主要,并且该地址必须至少具有1个与之关联的联系人。

An example business rule for a change set that contains a customer record and 1 or more address records, and/or 1 or more contact records: If the customer is assigned to Industry A then it must have exactly 1 address coded as "Primary" and that address must have at least 1 contact associated with it.

尝试通过评估现有状态和完整的更改集似乎需要大量工作并且容易出错。可能有多个地址/联系人被添加,删除或更新。在应用所有更新之后,提交之前,可以通过简单的SQL选择轻松完成此操作。

Trying to do that validation by evaluating the existing state and the complete set of changes seems like a lot of work and error prone. There could be multiple addresses/contacts being added, deleted, or updated. It could very easily be done by a simple SQL select after all the updates have been applied, prior to commit.

这是否可能?我有什么选择?
谢谢

Is this even possible? What are my options? thanks

推荐答案

好的,这确实需要更好地记录,但这应该提供开始:

Ok, this does need to be documented better but this should provide a start:

http://breeze.github.io/doc-net/persistencemanager-core.html

您可以将Breeze.AfterSaveEntities方法与使用TransactionScope的TransactionType的TransactionSettings实例。

You can use the Breeze.AfterSaveEntities method in conjunction with a TransactionSettings instance that uses a TransactionType of TransactionScope.

这可确保整个保存都在TransactionScope中进行(包括BeforeSaveEntities和AfterSaveEntities)。这意味着,如果在AfterSaveEntities方法内抛出一个Exception,它将中止该事务,并回滚该事务中以前的所有插入,更新或删除操作。

This insures that the entire save occurs within a TransactionScope (including the BeforeSaveEntities and the AfterSaveEntities). this means that if you throw an Exception inside of your AfterSaveEntities method, it will abort the transaction, and rollback any previous inserts, updates or deletes that were part of the transaction.

[HttpPost]
public SaveResult SaveWithTransactionScope(JObject saveBundle) {
  var txSettings = new TransactionSettings() { TransactionType = TransactionType.TransactionScope };
  ContextProvider.AfterSaveEntitiesDelegate = PerformPostSaveValidation;
  return ContextProvider.SaveChanges(saveBundle, txSettings);
}

private void PerformPostSaveValidation(Dictionary<Type, List<EntityInfo>> saveMap, List<KeyMapping> keyMappings ) {
  // do your validation stuff here
  // and throw an exception if something doesn't validate.

}

我希望这是有道理的:)

I hope this makes sense :)

这篇关于更新后但提交之前进行breezejs和实体框架验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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