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

  • Home
  • SEARCH
  • 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 6230925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:50:27+00:00 2026-05-24T09:50:27+00:00

I´m still having a hard time with Linq. I need to write a Update

  • 0

I´m still having a hard time with Linq.
I need to write a Update Function tat receives an object that has a list. Actually, A region has a list of cities. I want to pass an object “Region” that has a name filed and a list of cities. The problem, is the city objects came from another context and I am unable to attach them to this context. I have been trying several functions, and always get an error like “EntitySet was modified during enumeration” or other. I am tring to make the code below work, but if anyone has a different approach please help.

    public int Updateregion(region E)
    {
        try
        {
            using (var ctx = new AppDataDataContext())
            {
                var R =
                       (from edt in ctx.regiaos
                        where edt.ID == E.ID
                        select edt).SingleOrDefault();              
                if (R != null)
                {
                    R.name = R.name;
                    R.description = E.description;
                }
                R.cities = null;
                R.cities.AddRange(Edited.Cities);
                ctx.SubmitChanges();
                return 0 //OK!
            }
        }
        catch (Exception  e)
        {
            ......
        }
  • 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-24T09:50:28+00:00Added an answer on May 24, 2026 at 9:50 am

    You can’t attach objects retrieved from one datacontext to another, it’s not supported by Linq-to-SQL. You need to somehow dettach the objects from their original context, but this isn’t supported either. One can wonder why a dettach method isn’t available, but at least you can fake it by mapping the list to new objects:

    var cities = Edited.Cities.Select(city => new City {
        ID = city.ID,
        Name = city.Name,
        /* etc */
    });
    

    The key here is to remember to map the primary key and NOT map any of the relation properties. They must be set to null. After this, you should be able to attach the new cities list, and have it work as expected.

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

Sidebar

Related Questions

im trying my best to learn LINQ but im still having a hard time
I'm still having a hard time with this data encapsulation thing... I know that
I'm still having a hard time not wanting to use Tables to do my
I am having a really hard time attempting to debug LINQ to SQL and
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
I think I may need to re-think my design. I'm having a hard time
Still having a hard time grasping some of the harder templates any help would
I am still having a hard time wrapping my head around this. I want
I'm having a hard time with the submit handler... Why does the following still
I am having a hard time following this function. I do not understand how

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.