New Posts New Posts RSS Feed: Deleting entities
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Deleting entities

 Post Reply Post Reply
Author
sky40627 View Drop Down
Newbie
Newbie
Avatar

Joined: 06-Oct-2009
Posts: 34
Post Options Post Options   Quote sky40627 Quote  Post ReplyReply Direct Link To This Post Topic: Deleting entities
    Posted: 05-Jan-2011 at 4:34am
Hi,
 
I am trying to do the following
 
var q = _manager.UnpaidInvoices;
foreach (var unpaidInvoice in q)
  {
      unpaidInvoice.EntityAspect.Delete();
  }
_manager.SaveChanges();
 
when looking in the database the entities are deleted
 
but somehow they are still present in the entitymanager
 
because when i start adding the new entities i get an error saying that an entity with this key already exists.
 
foreach (var item in list)
{
   UnpaidInvoice ui = UnpaidInvoice.Create(_manager, item.Id);
}
_manager.SaveChanges();
 
also is it necessary to do the savechanges after the delete of the entities ?
 
here under is the create method
 
public static UnpaidInvoice Create(EntityManager pEntityManager, string pId)
        {
            UnpaidInvoice upi = pEntityManager.CreateEntity<UnpaidInvoice>();
            upi.Id = pId;
            upi.EntityAspect.AddToManager();
            return upi;
        }
 
what am I doing wrong here? Why isn't the cache update after the delete ?
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 05-Jan-2011 at 6:34pm
Hi sky40627,
 
I was not able to reproduce the issue here.
Here is my test case:
 
[TestMethod]
public void TestDelete() {
  var mgr = new IdeaBladeTest1Entities();
 
  var empToDelete = mgr.Employees.First();
  var id = empToDelete.Id;
  empToDelete.EntityAspect.Delete();
  mgr.SaveChanges(); // Persisting the delete
 
  var newEmp = mgr.CreateEntity<Employee>();
  newEmp.Id = id;
  newEmp.FirstName = "Joe" + DateTime.Now.Ticks.ToString();
  newEmp.LastName = "Doe";
  mgr.AddEntity(newEmp);
  mgr.SaveChanges(); // Storing the new employee
}
 
However, if you comment out the first mgr.SaveChanges() in the code then you will get the exception you described.
In That case, it happens because even though you have deleted the entity, you have not yet persisted the changes by calling mgr.SaveChanges.
 
Can you please provide a solution reproducing the issue?
 
On a second note, why are you trying to recycle PKs? Ideally, a primary key should be unique to one entity (regardless of if the entity has been deleted)
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down