none
Submit Changes/ DeleteOnSubmit not working but no error/ exception message RRS feed

  • Question


  • Hi,

    I am trying to delete an entity from my data context with no luck. I don't get an error message or an exception but the entity just never gets removed. Here's the code that is supposed to delete:

    DataRetrieveHelper:

    Code Snippet

            public static void Delete<T>(T entity) where T : class,new()
            {
                using (var db = new IntranetDBDataContext())
                {
                    Table<T> table = db.GetTable<T>();
                    table.Attach(entity);
                    table.DeleteOnSubmit(entity);
                    db.SubmitChanges();
                }
            }


    This is how it's called:

    Code Snippet

                using (var db = new IntranetDBDataContext())
                {

                    var announcement = db.Announcements.Single(an => an.AnnouncementID == AID);

                    DataRetrieveHelper.Delete<CathedralData.Announcement>(announcement);

                    return ActivityExecutionStatus.Closed;
                }



    The variables are all instantiated as I would expect. When on the debugger if each line has a breakpoint, the debugger stops at table.Attach() but never at table.DeleteOnSubmit or db.SubmitChanges().

    How can I further debug what's going on? Can anyone suggest a reason for this not to work? Please help I'm very stuck!

    I've checked my database for update/ delete rules- can't see anything that would cause a problem.

    Thanks,

    Lilalfyalien

    Wednesday, June 4, 2008 7:33 AM

Answers

  • Hi Lilalfyalien,

     

    I imitated the same code from your code and I am getting the exception below (probably it is the one that you aren't seeing for some reason) :

     

    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported"

     

    This, as in your code, is because if an object is already tracked by another data context and you are trying to attach it to a new one.

     

    What you can do is, you can amend your code a bit in a better way when you are calling delete:

     

    Code Snippet

    using (var db = new IntranetDBDataContext())
                {

                   db.ObjectTrackingEnabled = false;

                    var announcement = db.Announcements.Single(an => an.AnnouncementID == AID);

                    DataRetrieveHelper.Delete<CathedralData.Announcement>(announcement);

                    return ActivityExecutionStatus.Closed;
                }

     

     

    This works fine in my machine.

     

    Hope it helps,

     

    Sidar

    Wednesday, June 4, 2008 11:32 AM

All replies

  • Hi Lilalfyalien,

     

    I imitated the same code from your code and I am getting the exception below (probably it is the one that you aren't seeing for some reason) :

     

    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported"

     

    This, as in your code, is because if an object is already tracked by another data context and you are trying to attach it to a new one.

     

    What you can do is, you can amend your code a bit in a better way when you are calling delete:

     

    Code Snippet

    using (var db = new IntranetDBDataContext())
                {

                   db.ObjectTrackingEnabled = false;

                    var announcement = db.Announcements.Single(an => an.AnnouncementID == AID);

                    DataRetrieveHelper.Delete<CathedralData.Announcement>(announcement);

                    return ActivityExecutionStatus.Closed;
                }

     

     

    This works fine in my machine.

     

    Hope it helps,

     

    Sidar

    Wednesday, June 4, 2008 11:32 AM
  • I had the same problem when I am unit testing my delete record method (WCF Operation contract)

     

    I finally figured that I have enabled another unit test that actually saves the exact same record so net effect would be record still there

     

    So I confirm 

     

    table.DeleteOnSubmit(entity);
                    db.SubmitChanges();


    would work if the DB connection is made appropriately

    Thursday, December 9, 2010 1:07 AM