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 154111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:55:15+00:00 2026-05-11T09:55:15+00:00

edit: in case anyone is wondering, the actionhandler invokes code that creates and disposes

  • 0

edit: in case anyone is wondering, the actionhandler invokes code that creates and disposes the same kind of datacontext, in case that might have anything to do with this behaviour. the code doesn’t touch the MatchUpdateQueue table, but i figure i should mention it just in case.

double edit: everyone who answered was correct! i gave the answer to the respondent who suffered most of my questioning. fixing the problem allowed another problem (hidden within the handler) to pop up, which happened to throw exactly the same exception. whoops!

I’m having some issues with deleting items in LINQ. The DeleteOnSubmit call in the code below causes a LINQ Exception with the message ‘Cannot add an entity with a key that is already in use.’ I’m not sure what I’m doing wrong here, it is starting to drive me up the wall. The primary key is just an integer autoincrement column and I have no other problems until I try to remove an item from the database queue. Hopefully I’m doing something painfully retarded here that is easy to spot for anyone who isn’t me!

static void Pacman() {     Queue<MatchUpdateQueue> waiting = new Queue<MatchUpdateQueue>();      events.WriteEntry('matchqueue worker thread started');      while (!stop)     {         if (waiting.Count == 0)         {             /* grab any new items available */             aDataContext db = new aDataContext();             List<MatchUpdateQueue> freshitems = db.MatchUpdateQueues.OrderBy(item => item.id).ToList();              foreach (MatchUpdateQueue item in freshitems)                 waiting.Enqueue(item);             db.Dispose();         }         else         {             /* grab & dispatch waiting item */             MatchUpdateQueue item = waiting.Peek();             try             {                 int result = ActionHandler.Handle(item);                 if (result == -1)                     events.WriteEntry('unknown command consumed : ' + item.actiontype.ToString(), EventLogEntryType.Error);                  /* remove item from queue */                 waiting.Dequeue();                  /* remove item from database */                 aDataContext db = new aDataContext();                 db.MatchUpdateQueues.DeleteOnSubmit(db.MatchUpdateQueues.Single(i => i == item));                 db.SubmitChanges();                 db.Dispose();             }             catch (Exception ex)             {                 events.WriteEntry('exception while handling item : ' + ex.Message, EventLogEntryType.Error);                 stop = true;             }         }          /* to avoid hammering database when there's nothing to do */         if (waiting.Count == 0)             Thread.Sleep(TimeSpan.FromSeconds(10));     }      events.WriteEntry('matchqueue worker thread halted'); } 
  • 1 1 Answer
  • 2 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. 2026-05-11T09:55:15+00:00Added an answer on May 11, 2026 at 9:55 am

    You could do something to the effect of

    db.MatchUpdateQueues.DeleteOnSubmit(db.MatchUpdateQueues.Single(theItem => theItem == item)); 

    Just a note as other answers hinted towards Attach.. you will not be able to use attach on a context other then the original context the item was received on unless the entity has been serialized.

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

Sidebar

Related Questions

Just a little edit, in case it helps anyone out who happens to stumble
EDIT: In case the question below looks a bit 'extensive', the summary is that
EDIT: This code now works correctly, I only left it in case someone finds
EDIT: See my answer below--> I am wanting to have a view that when
I'm wondering what the correct way to compare two characters ignoring case that will
I have very similar multithreaded code elsewhere in my codebase that works fine, but
I can't get my tests to work and was wondering if anyone have any
I have a simple piece of code in a .NET console application that tries
I have a problem with a piece of legacy c++/winsock code that is part
I'm wondering if anyone using Jcarousel would have a suggestion on how to clone

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.