2009-09-21 57 views
0

我试图使用LinqToSQL执行一个直接更新,并且无法让它工作。LinqToSql更新切换父母时的问题

这是数据模型:有一个 timesheet_entry客户。一个客户有很多 timesheet_entries。这是一个简单的外键关系。

如果我正在编辑现有的timesheet_entry,如果我更改了customer_id,则会发生异常。

这是我对代码的尝试。有人能帮助我吗?

 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 

     } 

的错误是:“尝试已经取得了附加或添加,是不是新这是不支持的实体,或许已经从另一个DataContext的加载。”。

+0

你对很多SubmitChanges使用DataContext吗? – 2009-09-21 10:48:01

回答

0

你有没有理由不使用Attach方法?像下面这样:

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