使用 STE 在子对象中启用更改跟踪

发布于 2024-11-06 17:01:33 字数 510 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-11-13 17:01:33

您很可能将自我跟踪实体与 WCF 结合使用。那么就不需要手动启用变更跟踪。这已经为你完成了。生成 STE 的 T4 模板包含一个用 [OnDeserialized] 属性装饰的方法,该方法在实体被反序列化后开始跟踪(通常在到达客户端并从 WCF 为传输生成的 xml 转换回运行时类实例后发生)。请参阅确切的代码示例:

    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        IsDeserializing = false;
        ChangeTracker.ChangeTrackingEnabled = true;
    }

搜索您的实体或 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:

    [OnDeserialized]
    public void OnDeserializedMethod(StreamingContext context)
    {
        IsDeserializing = false;
        ChangeTracker.ChangeTrackingEnabled = true;
    }

Search your entities or the T4 template and you will find this soon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文