none
Mysterious System.NotSupportedException RRS feed

  • Question

  • Hey everyone,

    While working with Linq to SQL (a tool I'm relatively new with) I started getting this mysterious 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."

    Here is the code that is throwing/causing the exception:

    public void Save(Session session)
    {
           var dataContext = new MyDataContext();
           session.Guid = new Guid();
           dataContext.Sessions.InsertOnSubmit(session);
           dataContext.SubmitChanges();
    }
    The exception saying somethign about "Attaching" but, obviously, I'm not doing anything like that (that I'm aware of?). Can anyone provide any details on the cause here? If you need additional information please let me know!

    Thanks,
    Wednesday, March 10, 2010 6:12 AM

Answers

  • The problem is that you are presumably passing this function a Session you obtained from a different DataContext.

    It is generally advised that you use a single data context per unit-of-work rather than attempting to pass instances between DataContexts.

    [)amien
    Wednesday, March 10, 2010 6:17 AM
    Moderator
  • If you don't want to pass DCs around, one easy workaround is to add a rowversion/timestamp column to each of your tables. If there is a timestamp column that L2S can use for concurrency checks then you can detach and reattach entities across DCs. (If in a single layer app, the datacontractserializer also comes in handy for detaching entities, if n-tier with e.g. WCF then the serialization/deserialization that take place when the entity objects are passed around takes care of that part)
    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Wednesday, March 10, 2010 7:25 AM
    Answerer

All replies

  • The problem is that you are presumably passing this function a Session you obtained from a different DataContext.

    It is generally advised that you use a single data context per unit-of-work rather than attempting to pass instances between DataContexts.

    [)amien
    Wednesday, March 10, 2010 6:17 AM
    Moderator
  • Hey Damien,

    Thanks for the quick reply here. I've looked through my stack trace and associated code and it doesn't look like I am loading this session from anywhere. However, if it's significant, I am loading a User object that is assigned to this session before being saved. Could this have something to do with it?

    Any help you can provider here is great!

    Thanks,

    Wednesday, March 10, 2010 6:24 AM
  • Hello again Damien,

    After some more investigation it does indeed look like the issue is with regard to related objects. As previously mentioned I have an instance of a "User" object associated with this Session object (on a property called "User"). I load the user with 1 data context and I load the session with another. Really, what I need to be doing, is I need to pass around the data context it seems.

    I will experiment further and see if I can come up with a good architectural solution.

    Thanks,
    Wednesday, March 10, 2010 7:09 AM
  • Hey Damien,

    I was able to come up with a pattern that facilitates this rather easily. I use a RepositoryFactory to create each instance of a repository and pass the data context as a parameter in the constructor. Now each of my repositories will use that context!

    Thanks,
    Wednesday, March 10, 2010 7:16 AM
  • If you don't want to pass DCs around, one easy workaround is to add a rowversion/timestamp column to each of your tables. If there is a timestamp column that L2S can use for concurrency checks then you can detach and reattach entities across DCs. (If in a single layer app, the datacontractserializer also comes in handy for detaching entities, if n-tier with e.g. WCF then the serialization/deserialization that take place when the entity objects are passed around takes care of that part)
    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Wednesday, March 10, 2010 7:25 AM
    Answerer