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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:00:43+00:00 2026-05-17T07:00:43+00:00

This may have been asked already, but I can’t find it, so here goes.

  • 0

This may have been asked already, but I can’t find it, so here goes.

We have a generic read of a table, using Context.GetTable( ) and then attaching an expression to find the single record in the table. This table has associations. Unfortunately, we’re trying to change a field on which an association is made, so;

Foo is parent of Bar on FooId

When we’re going to change the FooId, it’s crashing because the HasAlreadyLoaded property of the EntityRef is set to true. Unless I’m not understanding LinqToSql well enough, I believe it’s the act of calling SingleOrDefault on this recordset that is actually firing the query and thus loading all the associated objects up.

Is there a way to stop this loading occuring? (ObjectTrackingEnabled and DeferredLoading set to true) and if not what is the common solution for this problem? We don’t know about individual relationships at this point, and I don’t want to have to look at the possibility of going figuring out the associations and loading them up based on the id.

My preferred method would have been a less generic one, and I could have done .Foo = Context.Foo.Select( x => x.Id == FooId ), but we don’t have that luxury now.

  • 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-17T07:00:44+00:00Added an answer on May 17, 2026 at 7:00 am

    In LINQ to SQL, to update an assocation property, you must move the child object from one parent’s EntitySet<> to another parent’s EntitySet<>, and during the move LINQ to SQL automatically updates the child’s association property.

    For example:

            // Get the source parent
            Foo foo1 = context.Foos.Where(f => f.ID == 1).SingleOrDefault();
            Bar bar1 = foo1.Bars[0];
    
            // (Note: you don't necessarily need to get foo1 first - 
            // you could just get bar1 directly and proceed from here...)
    
            // Get the destination parent
            Foo foo2 = context.Foos.Where(f => f.ID == 2).SingleOrDefault();
    
            // Move the child over.  LINQ to SQL automatically updates bar1.FooID from 1 to 2 here.
            foo2.Bars.Add(bar1);
    
            // Done.
            context.SubmitChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may have been already asked but I can't seem to find this specific
This question may have been asked already - but unfortunately, I could not find
this may have been asked already but I cant really find it (most likely
This may have been asked before, but I could not find the answer. I
this question may have been asked many times, but could not find any suitable
This question may well have been asked before but I didn't find anything whilst
This question has been asked before, but I can't find any answers that have
This may already have been asked, but StackOverflow is massive and trying to google
This may have been asked before and I just can't find it. I have
this may have been asked before, but I could not find it. Suppose I

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.