Silverlight WCF RIA,将项目添加到 EntityList 并提交域上下文更改
例如,我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您必须刷新缓存的客户端副本,以便新添加的 Id 字段存在。
正如概念证明,请尝试以下代码:
这将解决冲突,但不是正确的方法,最好在提交后再次刷新/加载查询。
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:
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.