views:

23

answers:

1

I am trying to perform a straighforward update using LinqToSQL, and just cannot get it to work.

Here's the data model: there is a timesheet_entry and customer. One customer has many timesheet_entries. It's a simple foreign key relationship.

If I'm editing an existing timesheet_entry, I get an exception if I change the customer_id.

Here's my attempt at the code. Can someone help me out here?

        internal void CommitChangesToEntry(Timesheet_Entry entry)
        {

            Timesheet_Entry latest = (from e
                                      in m_dataContext.Timesheet_Entries
                                      where e.Timesheet_Entry_ID == entry.Timesheet_Entry_ID
                                      select e).First();


            latest.Entry_Start_DateTime = entry.Entry_Start_DateTime;
            latest.Entry_End_DateTime = entry.Entry_End_DateTime;
            latest.Task_Description = entry.Task_Description;

            // Comment out this line of code and it
            // doesn't throw an exception
            latest.Customer_ID = entry.Customer_ID;

            m_dataContext.SubmitChanges(); // This throws a NotSupportedException

        }

The error 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".

A: 

Do you have any reason for not using the Attach method? Like the following:

m_dataContext.Timesheet_Entries.Attach(entry);
m_dataContext.SubmitChanges();
Konamiman