views:

391

answers:

2

Hi there,

can anyone help? I have an issue with linq2sql and trying to Attach (update) an entity class through a datacontext, it complains its not the original context, this is true (see code below) ... I thought it was best practices to open the datacontext and close it when not needed?

I basically have my app calling a service layer which in turn calls a repository where the db context is.... You can see here i have 2 data contexts, well a datacontext is defined in each method...

Basically what happens is my app "Gets" a reservation ... then the app updates the entity class "Reservation" and then resends it to "Updates" a Reservation.. Can anyone help.. I am completely stuck

here is basicallly my code

    public bool UpdateReservation(Reservation reservation)
    {
        bool success = false;
        try
        {
            ResDataContext db = new ResDataContext ();

            db.Reservations.Attach(reservation);
            db.SubmitChanges();
            success = true;

        }
        catch (Exception ex)
        {
            Console.WriteLine("");

        }


        return success;

    }

    public Reservation GetReservation(string reservationNumber)
    {
        ResDataContext db = new ResDataContext ();

        return db.Reservations.Where(r => r.ReservationNumber == reservationNumber).SingleOrDefault(); 
    }
A: 

You could first collect the correct Reservation object from the DataContext and then update and submit it.

Like this:

 public bool UpdateReservation(Reservation reservation)
{
    bool success = false;
    try
    {
        ResDataContext db = new ResDataContext ();

        Reservation res = db.Reservations.Where(r => r.ReservationNumber == reservation.ReservationNumber).Single();
        db.Reservations.InsertOnSubmit(res);
        db.SubmitChanges();
        success = true;

    }
    catch (Exception ex)
    {
        Console.WriteLine("");

    }


    return success;

}
Robban
thanks .. yes i see but in effect i am re-asking for the object for the new datacontext, although completely viable ... i didn't want to go back to the database just to get the new reservation which i already have but raised on a previous dataontext
mark smith
A: 

The problem is that you are not reattaching to the same data context.

You need to keep the ResDataContext alive between the calls.

  • Create a property on the class which contains ResDataContext.
  • Instanciate ResDataContext in the constructor
  • If your calls are going over WCF you will need to use per session calls

This was Eniity Framework (not linq to SQL), but the problem was similar:

http://stackoverflow.com/questions/1342266/entity-framework-attachasmodified-failure-confusion/1342786#1342786

Shiraz Bhaiji
but keeeping a reference to the datacontext i thought was a bad way to go.. I thought it was open datacontext - use it -- and destroy it??
mark smith
Shiraz, if in fact i do leave my datacontext as a class level variable in my repository then i think this would clear the issue as when i go to update the reservation - the datacontext is still alive.. But i woudl love to know what implications this would have by leaving the datacontext open all the time..
mark smith