使用 STE 在子对象中启用更改跟踪
我正在使用 STE,并且想要启用对象及其子对象的更改跟踪。我现在要做的就是这样的事情。
int id = 1;
using(CustomerEntities context = new CustomerEntities())
{
CustomerSection custSection = context.CustomerSections.Include("CustomerSections.Customers").SingleOrDefault(p => p.ID == id);
custSection.StartTracking();
foreach(Customer cust in custSection.Customers)
{
cust.StartTracking();
{
return custSection;
}
我正在寻找一种自动启用子对象更改跟踪的方法,而不必循环遍历每个对象并明确告诉它开始跟踪更改。
预先感谢您的任何见解。
I'm using STE and I want to enable change tracking for an object and its children. What I currently have to do now is something like this.
int id = 1;
using(CustomerEntities context = new CustomerEntities())
{
CustomerSection custSection = context.CustomerSections.Include("CustomerSections.Customers").SingleOrDefault(p => p.ID == id);
custSection.StartTracking();
foreach(Customer cust in custSection.Customers)
{
cust.StartTracking();
{
return custSection;
}
What I am looking for is a way to automatically enable change tracking for the child objects too, without having to loop through each one and explicitly tell it to start tracking changes.
Thanks in advance for any insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能将自我跟踪实体与 WCF 结合使用。那么就不需要手动启用变更跟踪。这已经为你完成了。生成 STE 的 T4 模板包含一个用 [OnDeserialized] 属性装饰的方法,该方法在实体被反序列化后开始跟踪(通常在到达客户端并从 WCF 为传输生成的 xml 转换回运行时类实例后发生)。请参阅确切的代码示例:
搜索您的实体或 T4 模板,您很快就会找到它。
Most probably you are using Self Tracking entities in combination with WCF. Then it's not needed to enable the changetracking manually. this is already done for you. The T4 template that generates the STE's includes a method decorated with the [OnDeserialized] attribute which starts the tracking once entities are deserialized (which occurs normally after reaching the client and converted back into runtime class instances fromout the xml that WCF generated for the transport. See the exact code example:
Search your entities or the T4 template and you will find this soon.