0

I am trying to write some business layer logic for an asp.net app to insert or update an object. I am getting an object out of the business layer and then passing it back in to be saved to the db. The data context is contained in the business layer which is what I think is causing the exception.

The exceptions is "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'm sure I am missing some small setting but I'm just not sure what.

this is the code that is doing the insertion and updating....

public static void Save(Order order)
{
        using (TicketInformationDataContext db = new TicketInformationDataContext())
        {                
            if (order.OrderID <= 0)
                db.Orders.InsertOnSubmit(order);
            else
            {
                db.ObjectTrackingEnabled = true;
                ITable table = db.GetTable(typeof(Order));
                table.Attach(order, true);
                db.Orders.Attach(order, true);
            }
            db.SubmitChanges();
        }
}
Jupaol
  • 21,107
  • 8
  • 68
  • 100
Kywillis
  • 707
  • 1
  • 8
  • 19
  • It looks like you are attaching the Order object twice to the same context I think... – mservidio Jun 01 '12 at 03:16
  • Try commenting out: ITable table = db.GetTable(typeof(Order)); table.Attach(order, true); – mservidio Jun 01 '12 at 03:16
  • I've tried commenting out `ITable table = db.GetTable(typeof(Order));` and `table.Attach(order, true);` and just having the `db.Orders.Attach(order, true);` I have also tried it the other way around with `db.Orders.Attach(order, true);` commented out and I get the same exception. – Kywillis Jun 01 '12 at 03:27
  • Is the Order object that you're creating and passing through to this method created using a different context that you instantiated somewhere else? – mservidio Jun 01 '12 at 03:38

1 Answers1

0

I believe your problem is that you are creating separate contexts for loading the Order and later for saving it.

You should design your repositories to work against a context you can pass during construction (or preferably inject through IoC). That way, both operations would work against the same context.

Remember that entities are bound to their context, and attempting to mix them will cause all sorts of problems, especially with lazy initialization or associated entities.

Please see the accepted response for this similar question, which discusses repository design and the unit of work design pattern.

Community
  • 1
  • 1
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
  • i understand this may be the standard method but for what ever the reason the design I am using is the way I need to get this to work (this is library is part of a much larger project). – Kywillis Jun 03 '12 at 23:04
  • I see...well in that case you are destined to run into trouble. Entities are not meant to be used in this manner, this should not cross contexts, for that you should have intermediate DTOs. The problem will also be related entities. One approach could be to use your entity "as if" it were a DTO. Meaning, on the new context, load the order by ID, and update/map each property, and reconfigure every relationship (also from the new context). Another approach might be to attempt it through reflection like: http://www.codeproject.com/Tips/57831/How-to-Attach-object-to-a-different-data-context – Pablo Romeo Jun 05 '12 at 16:15