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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:09:58+00:00 2026-05-13T19:09:58+00:00

I’m using the EF in a WinForms app, my idea is to have an

  • 0

I’m using the EF in a WinForms app, my idea is to have an ObjectContext per transaction instead of a single long running context. But I’m getting an error when I try to attach objects from a previous transaction into a new one, something to do with the entity already being in another context.

I kinda assumed that entities got detached when the object context gets disposed, is this not the case?? (Maybe I’m not disposing the context correctly somewhere). If entities don’t get detached, is there a way of doing so at disposal?

EDIT

Apparently entities are not being detached after context disposal as @F.Aquino said, but doing something like this seems to work. Although I’m not sure if this is a correct way of handling entities. Maybe someone could comment on problems that might arise from this:

public void Attach(params EntityObject[] objects)
{
    foreach (EntityObject obj in objects)
    {
        ((IEntityWithChangeTracker)obj).SetChangeTracker(null);
        entities.Attach(obj);
    }
}

Basically when I want to reattach an entity to a context, I just null the entity’s change tracker, and then just attach it to the new context. It seems to work fine.

  • 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-13T19:09:58+00:00Added an answer on May 13, 2026 at 7:09 pm

    You have to detach them manually and keep in mind all the references will be disposed in the process. There is this great magical class that deals with all the hassles on reattaching entities in EF 1, by Matthieu Mezil, usage would be something like:

    public static EntityObject SaveOrUpdate(this EntityObject entity)
    {
        using (MyEntities context = new MyEntities())
        {
            entity.AttachGraph(context, () => new MyEntities());
            context.SaveChanges();
            return entity;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.