Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

When you recreated your linq to sql file, did you remember to refresh the server browser to reflect the table name change first?

Edit - I mean the visual studio server browser.

you need to rebind your LINQ To SQL class, drop all the tables, then add them again from the Server Explorer

  1. open your project in visual studio.
  2. open your dbml file.
  3. find the table you just have renamed.
  4. select and delete it.
  5. Save.
  6. Open server Explorer.
  7. Connect to your database.
  8. Find the table which you have renamed.
  9. Drag and drop on the designer of your dbml file.
  10. Save the file again.
  11. Compile your project.

It is not necessary to drop the tables from your DBML file. You can simply open the designer, click on the class diagram representing your table and type the new name of the table.

However, if you're recreating from scratch it is important to refresh in Server Explorer otherwise it will still pull the old name from the schema.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top