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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:34:07+00:00 2026-05-14T06:34:07+00:00

In the dbml designer I’ve set Update Check to Never on all properties. But

  • 0

In the dbml designer I’ve set Update Check to Never on all properties. But i still get an exception when doing Attach: “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 approach seems to have worked for others on here, but there must be something I’ve missed.

        using(TheDataContext dc = new TheDataContext())
        {
            test = dc.Members.FirstOrDefault(m => m.fltId == 1);
        }

        test.Name = "test2";

        using(TheDataContext dc = new TheDataContext())
        {
            dc.Members.Attach(test, true);
            dc.SubmitChanges();
        }
  • 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-14T06:34:07+00:00Added an answer on May 14, 2026 at 6:34 am

    The error message says exactly what is going wrong: You are trying to attach an object that has been loaded from another DataContext, in your case from another instance of the DataContext. Dont dispose your DataContext (at the end of the using statement it gets disposed) before you change values and submit the changes. This should work (all in one using statement). I just saw you want to attach the object again to the members collection, but it is already in there. No need to do that, this should work just as well:

    using(TheDataContext dc = new TheDataContext())
    {
        var test = dc.Members.FirstOrDefault(m => m.fltId == 1);
        test.Name = "test2";
        dc.SubmitChanges();
    }
    

    Just change the value and submit the changes.

    Latest Update:

    (Removed all previous 3 updates)

    My previous solution (removed it again from this post), found here is dangerous. I just read this on a MSDN article:

    "Only call the Attach methods on new
    or deserialized entities. The only way
    for an entity to be detached from its
    original data context is for it to be
    serialized. If you try to attach an
    undetached entity to a new data
    context, and that entity still has
    deferred loaders from its previous
    data context, LINQ to SQL will thrown
    an exception. An entity with deferred
    loaders from two different data
    contexts could cause unwanted results
    when you perform insert, update, and
    delete operations on that entity. For
    more information about deferred
    loaders, see Deferred versus Immediate
    Loading (LINQ to SQL)."

    Use this instead:

    // Get the object the first time by some id
    using(TheDataContext dc = new TheDataContext())
    {
        test = dc.Members.FirstOrDefault(m => m.fltId == 1);
    }
    
    // Somewhere else in the program
    test.Name = "test2";
    
    // Again somewhere else
    using(TheDataContext dc = new TheDataContext())
    {
        // Get the db row with the id of the 'test' object
        Member modifiedMember = new Member()
        {
            Id = test.Id,
            Name = test.Name,
            Field2 = test.Field2,
            Field3 = test.Field3,
            Field4 = test.Field4
        };
        
        dc.Members.Attach(modifiedMember, true);
        dc.SubmitChanges();
    }
    

    After having copied the object, all references are detached, and all event handlers (deferred loading from db) are not connected to the new object. Just the value fields are copied to the new object, that can now be savely attached to the members table. Additionally you do not have to query the db for a second time with this solution.

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

Sidebar

Related Questions

I have a .dbml designer view, i have classes with properties, when i want
can I force the DBML designer to use my custom types instead of the
I'd like to use sqlmetal to generate dbml data contexts for my project, but
I'm using Visual Studio 2008 Each time I modify my dbml through the designer
I have a standard update happening via linq to sql but the data does
When I drag a particular stored procedure into the VS 2008 dbml designer, it
Can I just point the connection string in Dbml.designer.cs to the connectionstring in the
i am having a small problem here. The table which dbml (LinqToSql designer) is
I've got a new instance of an object in a Linq DBML, but don't
I have a linqtosql dbml where I dropped a stored procedure into the designer

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.