Suppose that I have two entities E1 and E2 both share the same Code value. Suppose that I want to delete the first and insert the next:
Item.Delete(E1);
Item.Insert(E2);
Item.Save();
Where
public void Delete(Entity E)
{
var existingEntity = _context.EntityTable.SingleOrDefault(s => s.Code == E.Code);
_context.EntityTable.Remove(existingEntity);
}
public void Insert(Entity E)
{
var existingEntity = _context.EntityTable.FirstOrDefault(s => s.Code == E.Code);
if (existingEntity != null){
throw new ArgumentException("Item alread exists.")
}
var newEntity = CreateDbEntity(E); // Create Db Entity just convert the type. Nothing much here.
_context.EntityTable.Add(newEntity);
}
public void Save()
{
_context.SaveChanges();
}
The problem here is that when I remove E1 from the EntityTable in the _context, that is not reflected immediately until I save. Thus, the operation will fail because inserting E2 sill not be successful since E1 is still there. Is there away work around that, where EntityTable does reflect the changes that have been made?