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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:12:04+00:00 2026-05-20T15:12:04+00:00

I am having a problem inserting and removing objects to/from the database using Linq

  • 0

I am having a problem inserting and removing objects to/from the database using Linq to SQL when utilitizing different Data contexts. The code works just fine as long as I insert and remove using the same context. However using the same context for each function is not an option as this will introduce concurrency problems as the code is used for a web service.

The code is written in F#, but it shouldn’t do any difference.

let CreateUser user =
    use context = new Rsvp(connectionString)
    context.Users.InsertOnSubmit(user)
    context.SubmitChanges()


let RemoveUserById user_id =
    use context = new Rsvp(connectionString)
    let user = GetUser user_id
    context.Users.DeleteOnSubmit(user)
    context.SubmitChanges()

An exeception is thrown from DeleteOnSubmit as follows:

System.InvalidOperationException: Cannot remove an entity that has not been attached.

Now you would think that you could just attach the object to the new data context. However doing so will cause another exception:

System.NotSupportedException: 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 am testing this from a C# test project with the following method

[TestMethod]
    public void Can_create_and_remove_a_new_user()
    {
        User user = new User();
        user.Name = "Test User";
        user.Email = "test@mail.com";
        user.Role_id = 1;

        UserModule.CreateUser(user);

        User inserted_user = UserModule.GetUserByEmail("test@mail.com");
        Assert.IsNotNull(inserted_user);

        UserModule.RemoveUserById(inserted_user.Id);
    }

Isn’t it possible to remove/insert the same object from different contexts?

EDIT:
A friend send me this article that could help to solve the problem but it doesn’t seem to be a definitive solution to the problem.

  • 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-20T15:12:05+00:00Added an answer on May 20, 2026 at 3:12 pm

    I think that’s just how DataContext works. An entity can belong only to a single data context and when you need to work with it for some time, you should keep the data context you used to retreive the entity.

    If you wanted to play with some nice functional ideas, this may be a good place to implement computation builder to keep the data context around (i.e. state monad). Then you could write:

    let CreateUser user = datacontext {
      let! context = getCurrentContext()
      context.Users.InsertOnSubmit(user)     
      context.SubmitChanges()  }
    

    Where getCurrentContext is an operation that returns the context that you’re passing around. There are probably a few examples how to implement state monad in F#. See for example this article.

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

Sidebar

Related Questions

Having a problem getting a TreeView control to display node images. The code below
We were having a problem with our build server not checking out modifications from
I'm having a problem writing Norwegian characters into an XML file using C#. I
I am having problem inserting a parameter into the top left hand corner of
I am fairly new to WPF and I am having a problem with inheriting
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
Having a problem trying to create a function, as part of a BizTalk helper
Im having a problem with a final part of my assignment. We get in
I'm having a problem with an application hanging and giving me the default Please

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.