Silverlight WCF RIA, adding items to EntityList and submitting domaincontext changes
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();
_Te开发者_StackOverflow中文版stDomainContext.Load(_TestDomainContext.GetTestItemsQuery());
Thank You !
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.
精彩评论