实体框架4:是否有一种方法来标记实体“仅插入” (不使用存储过程) [英] Entity Framework 4: Is there a way to mark an entity "insert-only" (without using stored procedures)

查看:95
本文介绍了实体框架4:是否有一种方法来标记实体“仅插入” (不使用存储过程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用v4实体框架作为新版本的应用程序的一部分,其中包括一些重要的会计功能(付款处理,现金平衡,不良支票处理等)。在我们以前的所有应用中,我们已经建立了至少有一张表,包含所有资金的审计跟踪,只能用新的数据写入政策(我被告知在银行/会计级应用程序中非常常见)。

换句话说,进入系统的新信息将触发一个新实体的创建,这将通过对我们所拥有的任何数据层对象的插入操作,并写入一个新行进入基础表。数据层有意实施,对这些实体进行更新或删除操作。任何更改都被跟踪为新插入的调整行。



使用EF,我们正在寻找一种完成相同目标的方法,但在这种情况下,我们显然需要阻止EF允许更新(而不是仅仅写入函数)。有没有办法将EF模型中的实体指定为仅插入?



正如我在标题中所提到的:我知道这可以通过映射存储插入操作的过程,而不是更新操作。我们正在寻找一些其他选项,如果存在,有两个原因:


  1. 我们还没有任何存储过程,如果我们可以避免,我们希望避免在我们的部署/升级过程中添加另一个元素。

  2. 此解决方案依赖于存储过程的不存在,表示该表是特殊的。我更喜欢一个解决方案,依赖于一个明确的断言,只插入是有意义的。


解决方案

你最好的选择是将.AsNoTracking添加到模型中的每个DbSet的getter中。这将意味着EF不会跟踪您从数据库获取的任何实体(因此不会对其执行任何回写)。另外还有提高性能的另外一个好处。



通过强制所有读取只读,您将消除EF中的更新路径。



查看我的帖子在AsNoTracking 在EF网站上的这篇文章


We are using the v4 Entity Framework as part of a new version of an application that includes some significant accounting functions (payment processing, cash balancing, bad check handling, etc.) In all of our previous applications, we have instituted a policy (which I'm told is extremely common in banking/accounting level applications) that there is at least one table, containing the incoming "audit trail" for all money, that can only be written to with new data.

In other words, new information coming in to the system would trigger the creation of a new entity, which would go through the "insert" operation of whatever data layer objects we had and write a new row into the underlying tables. The data layer was intentionally implemented with no update or delete actions for these entities; any changes were tracked as newly inserted adjusting rows.

With EF, we are looking for a way to accomplish the same goal, but in this case, we obviously need to prevent the EF from allowing the updates (as opposed to just not writing the functions). Is there some way to designate an entity in an EF model as being "insert only"?

As I mentioned in the title: I know this can be accomplished by mapping stored procedures to the Insert action but not the Update action. We are looking for some other option, if one exists, for two reasons:

  1. We don't have any stored procedures as yet, and would like to avoid adding another element to our deployment/upgrade process if we can avoid it.
  2. This solution relies on the "absence" of a stored procedure to indicate that the table is special. I'd prefer a solution that relied on a definitive assertion that the "insert-only" is that way on purpose.

解决方案

Yup your best bet is to add .AsNoTracking into the getters for each DbSet in your model. This will mean that EF wont be tracking any entities you get from the database (so wont perform any write backs on them). It also has the added bonus of improving performance.

By forcing all reads to be read only you will eliminate the update path in EF.

Take a look at my post on AsNoTracking, and this article on the EF site

这篇关于实体框架4:是否有一种方法来标记实体“仅插入” (不使用存储过程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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