0

I'm trying to delete a record from my table using EF, and nothing is happening. The code executes with no errors, but the record just sticks around in the DB. I have nearly identical code elsewhere that's working.

                using (var DB = new PTNWebConfigurationModel.PTNWebConfigurationEntities())
                {
                    var account = DB.Accounts.Where(a => a.LoginID == loginID).FirstOrDefault();


                        //Load existing session for the Account, otherwise create a new one.
                        if (!LoadExistingSession(ip, DB, account))
                        {
                            CreateNewSession(ip, DB, account);
                        }
                        AccountsSession sessionsToDelete = DB.AccountsSessions.Where(a => a.RemoteIP == ip && a.AccountID == 1).FirstOrDefault();
                        if (sessionsToDelete != null)
                        {
                            DB.DeleteObject(sessionsToDelete);
                            DB.SaveChanges();
                        }
                 }

I've also tried it with these options:

DB.DeleteObject(sessionsToDelete);
DB.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
DB.AcceptAllChanges();

I also started with no using block and just and instantiated entity object, but that didn't work either.

5
  • 1
    You don't have an enclosing TransactionScope that you're not completing, right? Commented Jan 7, 2011 at 17:48
  • You could try the trial of efprof.com Commented Jan 7, 2011 at 17:55
  • @Klaus: I fear we have this sort of thing all over the place. We're new to this. Can you describe what you mean? We try to enclose all our transaction in using blocks, but that's all we do to try and manage the scope. Commented Jan 7, 2011 at 19:14
  • 1
    I'm referring to if you have something like using(var txScope = new TransactionScope()) and you forget to call txScope.Complete(). Code like this wraps every method call inside the using block in a transaction, and if you don't call Complete changes will be rolled back. Commented Jan 7, 2011 at 20:30
  • We don't have any TransactionScope blocks. I'm wondering if we need them. I just read about a bug with EF and SqlServer less than 2008 where the auto-open and auto-close connection piece doesn't work properly. Commented Jan 7, 2011 at 20:32

2 Answers 2

1

I love issues like this.

Most of the times that I have seen an ORM not execute a command (and without error) is due to changes to the underlying data tables that aren't represented in the generated classes for that table.

You might refresh your schema / class model and try again.

Sign up to request clarification or add additional context in comments.

1 Comment

Good tip, and I'll keep it in mind. But it didn't work for this particular case. Still stumped.
0

Carrying on from Chris' answer the other thing I've found in the past is if you are doing EF database first modelling then you may have created the foreign keys linking two associated tables but the key hasn't been set as delete cascade in the relationship. This can cause EF to be a little difficult. So check any FKs in your db. Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.