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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:29:02+00:00 2026-05-31T22:29:02+00:00

In my previous applications when I used linq-to-sql I would always use one class

  • 0

In my previous applications when I used linq-to-sql I would always use one class to put my linq-to-sql code in, so I would only have one DataContext.

My current application though is getting too big and I started splitting my code up in different classes (One for Customer, one for Location, one for Supplier…) and they all have their own DataContext DatabaseDesignDataContext dc = new DatabaseDesignDataContext();

Now when I try to save a contact with a location (which I got from a different DataContext) I get the following error:

“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.”

I assume this is because I create a DataContext for every class, but I wouldn’t know how to this differently?

I’m looking for any ideas, thanks.

My classes look like the following:

 public class LocatieManagement
{
    private static DatabaseDesignDataContext dc = new DatabaseDesignDataContext();

    public static void addLocatie(locatie nieuweLocatie)
    {
        dc.locaties.InsertOnSubmit(nieuweLocatie);
        dc.SubmitChanges();
    }

    public static IEnumerable<locatie> getLocaties()
    {
        var query = (from l in dc.locaties
                     select l);
        IEnumerable<locatie> locaties = query;

        return locaties;
    }

    public static locatie getLocatie(int locatie_id)
    {
        var query = (from l in dc.locaties
                     where l.locatie_id == locatie_id
                     select l).Single();

        locatie locatie = query;
        return locatie;

    }
}
  • 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-31T22:29:03+00:00Added an answer on May 31, 2026 at 10:29 pm

    Another solution I found for this is to create one parent class DataContext

    public class DataContext
    {
        public static DatabaseDesignDataContext dc = new DatabaseDesignDataContext();
    }
    

    And let all my other classes inherit this one.

     public class LocatieManagement : DataContext
     {
            public static void addLocatie(locatie nieuweLocatie)
            {
                dc.locaties.InsertOnSubmit(nieuweLocatie);
                dc.SubmitChanges();
            }
     }
    

    Then all the classes use the same DataContext.

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

Sidebar

Related Questions

In my previous web development experience I have used LAMP setups. I am trying
I am new to .NET and LINQ to SQL. So far, I have been
In my previous company, we used to copy jsp files and the class files
I am developing one android application which use transparent activity.. I used the following
After seeing a previous post Making Applications programmed in .NET languages work on older
An extension of my previous thread here . I'm launching embedded resource applications via
I'm working through a previous webforms application to convert it to MVC and have
In a previous job we had a classic ASP application that no one wanted
I have inherited a application framework from the previous developer where I now work.
I would like to share one of my practical experience with multiprogramming here. Yesterday

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.