Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:38:09+00:00 2026-05-27T10:38:09+00:00

So I am trying to Delete some items from the database before I Insert

  • 0

So I am trying to Delete some items from the database before I Insert some data. For some reason, C# Entity Framework and ASP.NET do not enjoy me doing this simple operation and gives out another one of those vague errors.

I am trying to update a set of rows of information in a table (using foreach loops and Insert/Delete), as well as some properties in another table (using update)

How do I fix it? The error occurs on the Delete function, on the line:
this.entities.tableModule.Attach(module);

I get: “System.InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.”

public void Insert(tableModule module)
    {
        this.entities.tableModule.AddObject(module);
        this.entities.SaveChanges();
    }    
public void Delete(tableModule module)
    {
        this.entities.tableModule.Attach(module); // error HERE
        this.entities.DeleteObject(module);
        this.entities.SaveChanges();
    }

Further down the code I call Delete and Insert and Update for another table.

try
        {
            DifferentClass.Update(tu);
            foreach (tableModule mitem in list_to_del)
            {
                moduleclass.Delete(mitem); // remove all super roles by the user.
            }
            foreach (tableModule m_to_add in add_list)
            {
                moduleclass.Insert(m_to_add);
            }
        }

From what I gather, the attach function is maybe being run more than once and that for some reason is something that the designers of C# Entity Framework are completely caught by surprise for, and so they produce an error. Though it’s most likely my complete lack of understanding of entity framework.

EDIT:
Here’s how I get the list_to_del array.

List<tableModule> list_to_del = (from m in entities.tableModule where m.userid == (long)userID select m).ToList(); 

EDIT2:

New test code:

public void InsertQueue(tableModule module)
{
    this.entities.tableModule.AddObject(module);
}
public void DeleteQueue(tableModule module)
{
    this.entities.tableModule.Attach(module);
    this.entities.DeleteObject(module);
}
public void FinallySave(){
    this.entities.SaveChanges();
}

EDIT 3 —- SOLUTION —–

The test code worked sort of (though inserts didn’t for some odd reason). The problem was, I was calling list_to_del query to get that array of tableModules from a tableUser class which has its own entityset/context. So the results in list_to_del were of a different context, than the call to the class that does DeleteQueue / Insert etc.

I changed:

List<tableModule> list_to_del = (from m in entities.tableModule where m.userid == (long)userID select m).ToList(); 

to the appropriate class that has the SAME query:

List<tableModule> list_to_del = ModuleClass.GetListofModulesByUserID(userID);

Of course ModuleClass.InsertQueue and ModuleClass.DeleteQueue is called.

So everything uses the same entities object (context/entityset).

So the error happens when you call a query on a context, that returns a set of results that are ALREADY attached to that context. Then when you call Attach again, a problem occurs because they already are in a context on some other class/instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:38:10+00:00Added an answer on May 27, 2026 at 10:38 am

    You don’t need to call Attach if you loaded module from the same context instance – you can call DeleteObject directly. When you load entity from the context it is by default tracked by the context (= it is attached) and you cannot attach it again. You also cannot attach it to different context so make sure that list_to_del is loaded by the same context instance as you use for deletion (= it must be loaded in the same request processing because you should have a new context instance per request).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
I am trying to delete several rows from a MySQL 5.0.45 database: delete from
I'm trying to remove a row from my gridview manually, because for some reason,
I'm new to MVC. Trying out some sample code to list items from db.
I am trying to get some data from page to page and then mail
i am trying to delete some files using a batch file.. (winxp) my problem
I'm trying to delete some files with unicode characters in them with batch script
I'm trying to write some SQL that will delete files of type '.7z' that
I'm trying to make sure some data is auto-deleted when there's no more references
I'm trying to delete a directory that contains XML files from a remote computer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.