LINQ not happy attaching entity loaded from another DataContext RRS feed

  • Question

  • I have database tables (and therefore LINQ data objects) Ingredient and IngredientType.  An Ingredient may have but one type.  An IngredientType may be applied to zero or many Ingredients.

    In LINQ (and we're using LINQ to SQL), I'm doing this:

    Dim ingredient As New Ingredient()
    ingredient.IngredientId = New Guid()
    ingredient.IngredientName = "Corn"

    Dim ingredientType As New IngredientType()
    ingredientType.IngredientTypeId = New Guid()
    ingredientType.IngredientTypeName = "Vegetable"


    But when I call the LINQ submit changes method, I get:

    ex = {"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."}

    Does anyone know how to make LINQ happy (i.e. that it works with the same data context) so this will work?


    Ronald S. Cook, MCSD, MCT
    Monday, January 12, 2009 4:36 PM

All replies

  • Do you have a call to your datacontext instance to add the ingredient ?


    Dim myDataContext as New MyDataContext()

    Tuesday, February 3, 2009 8:36 AM
  • In addition to what Damien asked, I think you are missing some code in your example that would explain the error. That error typically occurs when the deferred relationship loaders have already been initialized for an entity in the context. But in your example, you are creating everything brand new, so even if you aren't calling InsertOnSubmit, if there were no other entities in the context, you should not get any errors on SubmitChanges (but of course without InsertOnSubmit, your new entities wouldn't  be inserted, either). Can you provide some more code that shows what else you are doing with the DataContext before and after the code you showed? Also, you can try to use the GetChangeSet method to get an idea of what the DataContext thinks it is going to do during SubmitChanges. That may give you a clue if there are any other objects that the context is trying to process.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 5, 2009 11:36 PM
  • This was bugging me as well. I did some searching and found a lot of confusion and some sloppy work-arounds regarding detached entities. I then found a nice solution on codeplex that has solved the immediate issue and has greatly extended linq2sql functionality. Its a small class thats really easy to implement and forms an EntityBase for your projects dbml's.

    Here is the offical description and link.
    LINQ to SQL Entity Base is a simple base class that is primarily designed to support LINQ to SQL in a disconnected way, which is one of the shortcomings of the LINQ to SQL technology at the present time. This is highly useful in an n-Tier, distributed or ASP.NET environment where disconnected functionality is relavent.

    Hope this helps anyone who is having similar.

    Jerome Vernon

    Tuesday, September 15, 2009 3:23 AM