1
votes

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 !

1
is Id an auto increment in the db?TBohnen.jnr
jep. id is autoincrement in dbshkipper

1 Answers

2
votes

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.