SubmitChanges 完成后,DomainContext 有时仍具有 HasChanges

发布于 2024-09-02 13:47:22 字数 2696 浏览 1 评论 0原文

我有一个非常简单的服务器模型,其中包括一个父实体和一个子实体的 [Composition] 列表。在我的客户端中,我有两个功能。一个函数从父实体中删除所有子实体,另一个函数删除所有子实体并编辑父实体的属性。

当我简单地删除所有子实体和 SubmitChanges() 时,一切都很好。

当我删除所有子实体并编辑父实体和 SubmitChanges() 时,在触发 SubmitChanges() 回调时仍然存在挂起的更改 (HasChanges == true)。

我正在使用 Silveright 4 RTM 和 RIA Services 1.0 RTM。

有什么想法吗?

以下是服务器实体:

public class RegionDto
{
    public RegionDto()
    {
        Cities = new List<CityDto>();
    }

    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    [Include]
    [Composition]
    [Association("RegionDto_CityDto", "Id", "RegionId")]
    public List<CityDto> Cities { get; set; }
}

public class CityDto
{
    [Key]
    public int Id { get; set; }

    public int RegionId { get; set; }
    public string Name { get; set; }
}

以下是客户端代码:

public static class CState
{
    private static RegionDomainContext _domainContext;

    public static RegionDomainContext DomainContext
    {
        get
        {
            if (_domainContext == null)
            {
                _domainContext = new RegionDomainContext();
            }
            return _domainContext;
        }
    }

    public static void SaveChanges()
    {
        DomainContext.SubmitChanges(op =>
        {
            if (DomainContext.HasChanges && !DomainContext.IsSubmitting)
            {
                var w = new ChildWindow();
                w.Content = "The DomainContext still has unsaved changes.";
                w.Show();
            }
        }, null);
    }
}

public partial class MainPage : UserControl
{
    private void ClearCitiesEditRegion(object sender, RoutedEventArgs e)
    {
        var region = (RegionDto)regionList.SelectedItem;

        if (region != null)
        {
            region.Name += "*";
            while (region.Cities.Count > 0)
            {
                region.Cities.Remove(region.Cities.First());
            }

            CState.SaveChanges();
        }
    }

    private void ClearCities(object sender, RoutedEventArgs e)
    {
        var region = (RegionDto)regionList.SelectedItem;

        if (region != null)
        {
            while (region.Cities.Count > 0)
            {
                region.Cities.Remove(region.Cities.First());
            }

            CState.SaveChanges();
        }
    }
}

当您运行此代码时,仅在调用 ClearCitiesEditRegion() 方法时才会显示 ChildWindow。此方法与 ClearCities() 方法之间的唯一区别是我编辑region.Name 属性的行。

您还可以在此处下载重现此内容的示例项目:http://dl.dropbox.com /u/2393192/RIA_Services_Problem.zip

I have a very simple server model that includes a parent entity with a [Composition] list of child entities. In my client, I have 2 functions. One function removes all the child entities from the parent and the other removes all and also edits a property on the parent entity.

When I simply remove all child entities and SubmitChanges(), all is well.

When I remove all child entities and edit the parent and SubmitChanges(), there are still pending changes (HasChanges == true) when the SubmitChanges() callback is fired.

I am using Silveright 4 RTM and RIA Services 1.0 RTM.

Any ideas what is going on here?

Here are the server entities:

public class RegionDto
{
    public RegionDto()
    {
        Cities = new List<CityDto>();
    }

    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    [Include]
    [Composition]
    [Association("RegionDto_CityDto", "Id", "RegionId")]
    public List<CityDto> Cities { get; set; }
}

public class CityDto
{
    [Key]
    public int Id { get; set; }

    public int RegionId { get; set; }
    public string Name { get; set; }
}

And here is the client code:

public static class CState
{
    private static RegionDomainContext _domainContext;

    public static RegionDomainContext DomainContext
    {
        get
        {
            if (_domainContext == null)
            {
                _domainContext = new RegionDomainContext();
            }
            return _domainContext;
        }
    }

    public static void SaveChanges()
    {
        DomainContext.SubmitChanges(op =>
        {
            if (DomainContext.HasChanges && !DomainContext.IsSubmitting)
            {
                var w = new ChildWindow();
                w.Content = "The DomainContext still has unsaved changes.";
                w.Show();
            }
        }, null);
    }
}

public partial class MainPage : UserControl
{
    private void ClearCitiesEditRegion(object sender, RoutedEventArgs e)
    {
        var region = (RegionDto)regionList.SelectedItem;

        if (region != null)
        {
            region.Name += "*";
            while (region.Cities.Count > 0)
            {
                region.Cities.Remove(region.Cities.First());
            }

            CState.SaveChanges();
        }
    }

    private void ClearCities(object sender, RoutedEventArgs e)
    {
        var region = (RegionDto)regionList.SelectedItem;

        if (region != null)
        {
            while (region.Cities.Count > 0)
            {
                region.Cities.Remove(region.Cities.First());
            }

            CState.SaveChanges();
        }
    }
}

When you run this code the ChildWindow is only shown when you the ClearCitiesEditRegion() method is called. The only difference between this and the ClearCities() method is the line where I edit the region.Name property.

You can also download a sample project that reproduces this here: http://dl.dropbox.com/u/2393192/RIA_Services_Problem.zip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

幸福%小乖 2024-09-09 13:47:22

我在 Silverlight 论坛上收到了对此的答复。显然这是 RIA Service 1.0 中的一个错误。以下是 Matthew 在 Silverlight 论坛上的回复

是的,我已经确认这是一个错误。
感谢您报告并提供
复制品。正如您所发现的,该错误
只会在成分上重现
父母去过的场景
除了一项或多项之外还进行了修改
孩子们。解决方法是做一个
显式 AcceptChanges 如果提交
成功了。例如,这里是
您在提交中编写的代码
回调:

   if (!submitOperation.HasError)
   {
      ((IChangeTracking)ctxt.EntityContainer).AcceptChanges();
   }

这将接受所有更改并重置
正确改变状态。

I received an answer to this on the Silverlight forums. Apparently this is a bug in RIA Service 1.0. The following is Matthew's response on the Silverlight forums.

Yes, I've confirmed this is a bug.
Thanks for reporting it and providing
the repro. As you discovered, the bug
will only repro in composition
scenarios where the parent has been
modified in addition to one or more
children. The workaround is to do an
explicit AcceptChanges if the submit
was successful. For example, here is
the code you'd write in a submit
callback:

   if (!submitOperation.HasError)
   {
      ((IChangeTracking)ctxt.EntityContainer).AcceptChanges();
   }

This will accept all changes and reset
change state correctly.

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