Silverlight WCF RIA,将项目添加到 EntityList 并提交域上下文更改

发布于 2024-11-10 13:57:15 字数 812 浏览 7 评论 0原文

例如,我有一个 TestItem 实体:

public class TestItem
{
    [Key]
    public int Id { get; set; }
    public string Description { get; set; }
}

以及带有列表的视图模型、我们将向此列表添加新项目的方法以及我们将调用 _TestDomainContext.SubmitChanges 的方法

EntityList<TestItem> SomeList = new EntityList<TestItem>(_TestDomainContext.TestItems);

private void AddTestItem()
{
    SomeList.Add(new TestItem());
}

private void SubmitChanges()
{
    _TestDomainContext.SubmitChanges();
}

现在,在将第一个项目添加到列表之后, SubmitChanges() 被称为一切正常,但是当我尝试添加第二个项目时,我得到了异常: 此 EntitySet 中已存在具有相同标识的实体。

摆脱这个问题的唯一方法是在 OnSubmitComplete 回调中手动刷新 SomeList 即:

_TestDomainContext.TestItems.Clear();
_TestDomainContext.Load(_TestDomainContext.GetTestItemsQuery());

谢谢!

for example i've got a TestItem entity:

public class TestItem
{
    [Key]
    public int Id { get; set; }
    public string Description { get; set; }
}

and view model with list, method where we'll add new items to this list, and method where we'll call _TestDomainContext.SubmitChanges

EntityList<TestItem> SomeList = new EntityList<TestItem>(_TestDomainContext.TestItems);

private void AddTestItem()
{
    SomeList.Add(new TestItem());
}

private void SubmitChanges()
{
    _TestDomainContext.SubmitChanges();
}

And now, after the first item is added to list and SubmitChanges() is called everything works perfectly, but when i try to add second item i get the exception :
An entity with the same identity already exists in this EntitySet.

is the only way to get rid of this is manually refresh the SomeList in OnSubmitComplete callback i.e.:

_TestDomainContext.TestItems.Clear();
_TestDomainContext.Load(_TestDomainContext.GetTestItemsQuery());

Thank You !

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

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

发布评论

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

评论(1

醉态萌生 2024-11-17 13:57:15

是的,您必须刷新缓存的客户端副本,以便新添加的 Id 字段存在。

正如概念证明,请尝试以下代码:

private void AddTestItem()
{    
    var key = _TestDomainContext.TestItems.Max(c=> c.Id) + 1;
    SomeList.Add(new TestItem(){Id = key});
}

这将解决冲突,但不是正确的方法,最好在提交后再次刷新/加载查询。

Yes, you have to refresh your client copy of the cache so that the newly added Id fields are there.

Just as a proof of concept try the below code:

private void AddTestItem()
{    
    var key = _TestDomainContext.TestItems.Max(c=> c.Id) + 1;
    SomeList.Add(new TestItem(){Id = key});
}

This will resolve the conflict but is not the right way of doing it, it is the best to refresh/load the query again after a submit.

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