改变跟踪api的例子 [英] change tracking api example

查看:60
本文介绍了改变跟踪api的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有针对新EF4 CTP5的跟踪api使用或文档的更改?


我的方案有两个具体:


1 。 审计目的 - 保存旧值和新值


2。记录版本控制 - 能够回滚到特定版本的表格(这些版本可能跨越相关对象)。 例如,更改子记录会触发任何父记录中的修订以映射到子项的新值。


谢谢,

-Bernie

解决方案

Bernie,


 


我们正在筹划一篇涵盖变更跟踪API的博客文章,但我会尝试在此处回答您的具体问题。


 


1。 EF会跟踪每个实体的原始值和当前值。 
原始值是实体从数据库中查询时的值。 
当前值是实体现在拥有的值。 
EF不会跟踪更多的历史记录。


 


您可以将数据库中的原始值,当前值和值作为类字典对象访问:


 


   
var
currentValues = context.Entry(entity).CurrentValues;


    ; 
var
originalValues = context.Entry(entity).OriginalValues;


    ; 
var
databaseValues = context.Entry(entity).GetDatabaseValues();


 


然后,您可以从这些词典中访问各个属性值,创建值的副本,将所有原始值复制到当前值,等等。


 


您还可以使用属性API访问属性的当前值和原始值。 
例如:


 


   
var
currentNameValue = context.Entry(entity).Property(e => e.Name )。CurrentValue;


   
var
originalNameValue = context.Entry(entity).Property(e => e.Name ).OriginalValue;


 


1。通常,EF不支持回滚到特定版本。 
您可以通过执行以下操作将实体回滚到上次从数据库查询时所具有的值:


 


   
var
entry = context.Entry(entity);


     ;
entry.CurrentValues.SetValues(entry.OriginalValues);


    
entry.State =
EntityState
.Unchanged;


 


这不会触发相关实体的任何更改,因此您必须自己管理。 
此外,如果希望通过这样做重置关系,则应在对象模型中包含外键。


 


希望有所帮助,即使它并不完全符合您的要求。


 


谢谢,


Arthur


Is there a change tracking api usage or documentation for the new EF4 CTP5?

My scenarios are two specifically:

1. Audit purposes - saving old and new values

2. record versioning - being able to roll-back to specific version of tables (these may span across related objects).  For example changes the child records would trigger a revision in any parent records to map to the new values of the child.

thanks,
-Bernie

解决方案

Bernie,

 

We are planning a blog post covering the change tracking API, but I will try to answer your specific questions here.

 

1. EF keeps track of original and current values for every entity.  The original values are the values the entity had when it was queried from the database.  The current values are the values the entity has now.  EF doesn’t keep track of any more history than this.

 

You can access the original values, current values, and values in the database as dictionary-like objects:

 

    var currentValues = context.Entry(entity).CurrentValues;

    var originalValues = context.Entry(entity).OriginalValues;

    var databaseValues = context.Entry(entity).GetDatabaseValues();

 

You can then access individual property values from these dictionaries, create copies of the values, copy all original values into current values, and so on.

 

You can also access current and original values for a property using the property API.  For example:

 

    var currentNameValue = context.Entry(entity).Property(e => e.Name).CurrentValue;

    var originalNameValue = context.Entry(entity).Property(e => e.Name).OriginalValue;

 

1. In general EF doesn’t support rolling back to specific versions.  You can rollback an entity to the values it had when it was last queried from the database by doing something like:

 

    var entry = context.Entry(entity);

    entry.CurrentValues.SetValues(entry.OriginalValues);

    entry.State = EntityState.Unchanged;

 

This won’t trigger any changes in related entities so you would have to manage that yourself.  Also, you should include foreign keys in your object model if you want relationships to be reset by doing this.

 

Hope that helps somewhat even if it’s not exactly what you were looking for.

 

Thanks,

Arthur


这篇关于改变跟踪api的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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