none
The DeleteObject() method doesn't seem to be working, why? RRS feed

  • Question

  • I've got an EF 4.2 DLL I wrote a while ago, which we are now using extensively. (This just started yesterday). I used what I thought would be the appropriate code to select a record from a table and delete it, but I'm finding that the record is still there. Here's the code:

    using (ASIEntities context = new ASIEntities())
    {
    	var asiCheckOut = context.ASICheckOuts.Where(a => a.ClientNumber == ClientNumber &&
    		 a.CaseNumber == CaseNumber).FirstOrDefault();
    	if (asiCheckOut != null)
    	{
    		context.DeleteObject(asiCheckOut);
    		context.SaveChanges();
    	}
    }
    

    Have I misunderstood how this works, or have left something out?


    Rod

    Friday, January 24, 2014 6:23 PM

All replies

  • Hello,

    >>Have I misunderstood how this works, or have left something out?

    I made a test that is similar with yours:

    Order order = db.Orders.Where(o => o.OrderID == 3).FirstOrDefault();
    
    
                        if (order != null)
    
                        {
    
                            db.DeleteObject(order);
    
    
                            db.SaveChanges();
    
                        }
    

    It worked fine, so I think the code is completely ok.

    Do you have a try to add a breakpoint and to debug to see whether the asiCheckOut object is null or not?

    When you check the database, do you refresh it?

    And please have a try to use try-catch block to check if it throws errors in actual.

    If this does not work for you, could you please upload your application and your related table schema to the skydriver

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 27, 2014 2:27 AM
    Moderator
  • I do have a try-catch block which I wrap around the using(...) statement (I left it off).

    How do I refresh the database? That sounds promising, because I don't think I'm doing that.


    Rod

    Monday, January 27, 2014 10:12 PM
  • Hello,

    >> How do I refresh the database?

    If you are using SQLServer2012, there is a “!”, after we do some operation on tables. We need to click it:

    More effect way to write a T-SQL to check whether the record could be found any more.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 28, 2014 8:56 AM
    Moderator
  • Hello,

    >> How do I refresh the database?

    If you are using SQLServer2012, there is a “!”, after we do some operation on tables. We need to click it:

    More effect way to write a T-SQL to check whether the record could be found any more.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Do you have to be on the server that hosts SQL Server, in order to run this debugging? I ask because this has never worked for me.

    Rod

    Friday, January 31, 2014 6:24 PM
  • >>Do you have to be on the server that hosts SQL Server, in order to run this debugging?

    No, my test is done with a local db. Have a try to work with a  localdb to check whether it will also strike.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, February 3, 2014 9:03 AM
    Moderator