asp.netsql-serverlinq-to-sqltable-rename

The Ghost of a Renamed Table


I have renamed a table in a SQL Server 2008 database, from eL_CourseStepUserNotes to StepUserNotes. I renamed the table using the SSMA.

The table is used in a ASP.NET 4.0 app. I use LINQ to SQL for all CRUD. The problem is that the following code:

    dbDataContext db = new dbDataContext();
    var k = (from c in db.StepUserNotes
             where ((c.CourseStepFK == q.CourseStepFK) && (c.UserFK == q.UserFK))
             select c).FirstOrDefault();
    try
    {
        db.StepUserNotes.InsertOnSubmit(q);
        db.SubmitChanges();
    }
    catch
    {

    }

Fails on the db.SubmitChanges line, saying:

SqlException was caught. Invalid object name 'eL_CourseStepUserNotes'.

Ie, the old name for the table has come back to haunt me.

I have deleted the old LINQ to SQL dbml file and created a new one. I have searched through all the source code for strings that contain the old table name. Nothing. The code compiles...

Where else can I look?

The error is coming back from SQL Server, and using the utility for listing all foreign keys in a sql server database shown in SO question: sql:need to change constraint on rename table?

reveals no sign of the old table name in FKs either.

I am at a complete loss as to where to look or what to try next. Any suggestions?


Solution

  • Answer:

    The problem, as stated by Stu and Stark was a trigger. Stu gave me the SQL to run that nailed the problem. It is documented here in case anyone else runs into this:

    Select Object_Name(ID) From SysComments 
           Where Text Like '%el_CourseStepUserNotes%'
    

    This revealed a trigger with the following name:

    tr_eL_CourseStepUserNotes
    

    The trigger referenced the old name as follows:

    SET DateAmended = CURRENT_TIMESTAMP
         FROM eL_CourseStepUserNotes PP  
                INNER JOIN inserted i  ON PP.UserNoteId = i.UserNoteId
    

    Everything is now working again.

    Silly me, I should have realised that a trigger was the problem as the first error I got was related to the DateAmended field.

    I have no idea why a trigger would update when a table name changed. I had checked all Keys and relationships, but forgot this trigger.

    Live and learn.