如何跟踪与WCF RIA服务实体的变化? [英] How to track entity changes with WCF Ria Services?
问题描述
我需要登录到客户端上发生的一个数据库表中的实体所做的更改。然而,当我修改和检查ChangeTracker属性,它声称没有发生变化。
I need to log changes made to an entity that occur on the client to a database table. However, when I make changes and inspect the ChangeTracker property, it claims that no changes have occurred.
[服务]
private static readonly ISomeDao someDao = DataAccess.SomeDao;
[Query]
public List<SomeEntity> GetSomeEntites(int someId)
{
var entities = someDao.GetSomeEntites(someId);
entities.ForEach(e => e.StartTracking());
return entities;
}
[Update]
public void UpdateSomeEntity(SomeEntity entity)
{
// inspect entity.ChangeTracker.OriginalValues.... nothing
entity.StopTracking();
// inspect entity.ChangeTracker.OriginalValues.... nothing
...
// Update log table
}
[客户]
public EntitySet<SomeEntity> SomeEntities
{
get { return _someEntity; }
set
{
if (_someEntity!= value)
{
_someEntity= value;
OnPropertyChanged("SomeEntities");
}
}
}
所以,如果,自我跟踪实体和WCF RIA Services不能生活在一起(这就是我怀疑),如何做一个轨道的变化?
So, if, self-tracking entities and WCF Ria Services can't live together (which is what I'm suspecting), how does one track changes?
推荐答案
不要用RIA服务工作,但单独与WCF(不知道它使太大的区别...)。首先你没有执行命令(服务器端),以StartTracking这是通过WCF与反序列化自动完成的实体。
Don't work with Ria services but solely with WCF (don't know if it makes much difference...). First of all you don't have to execute the command (server-side) to StartTracking the entities this is done automatically with deserializing by WCF.
[OnDeserialized]
public void OnDeserializedMethod(StreamingContext context)
{
IsDeserializing = false;
ChangeTracker.ChangeTrackingEnabled = true;
}
其次,我知道的是,一个自我的唯一小学和外键的属性改变时跟踪实体被记录。这是不是为保持生活在一个实体上下文(那么所有的修改记录)NORMAL实体的情况下。
Second what I know is that only primary- and foreign key properties of a SELF tracking entity are recorded when changed. this is not the case for NORMAL entities that keep living in an entity context (then all changes are recorded).
如果您需要在自我跟踪 - 细看实体您可能会看到这样的事情:
If you take a closer look at a Self-Tracking-Entity you might see something like this:
[DataMember]
public string Plaats
{
get { return _plaats; }
set
{
if (_plaats != value)
{
_plaats = value;
OnPropertyChanged("Plaats");
}
}
}
private string _plaats;
[DataMember]
public int LandID
{
get { return _landID; }
set
{
if (_landID != value)
{
ChangeTracker.RecordOriginalValue("LandID", _landID);
if (!IsDeserializing)
{
if (Land != null && Land.ID != value)
{
Land = null;
}
}
_landID = value;
OnPropertyChanged("LandID");
}
}
}
private int _landID;
你看有什么区别?简单物业国家地点和外键属性LandID之间?这是该行`ChangeTracker.RecordOriginalValue(LandID,_landID);
Do you see the difference? between the simple property Plaats and the Foreign key property LandID? It's in the line `ChangeTracker.RecordOriginalValue("LandID", _landID);
有关简单的属性这些变化并不被记录(属性themseklves是场外改变,所以。这EF背景下知道如何ApplyChanges和更新数据库)
For simple properties these changes are not recorded (the properties themseklves are off-course changed, so that EF context knows how to ApplyChanges and update the database).
可能的想法可能是:
- 要自定义T4模板来记录原始值的每个属性
- 要从基类,你可以放下一些框架,通过响应记录原始值派生的所有实体的PropertyChanged事件
- 当更新的entites,第一次获得来自数据库的原始值和跟踪更改
希望这有助于!
这篇关于如何跟踪与WCF RIA服务实体的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!