Search code examples
c#sqlsql-servercode-first

ALTER TABLE DROP COLUMN failed because one or more objects access this column


I am trying to do this:

ALTER TABLE CompanyTransactions DROP COLUMN Created

But I get this:

Msg 5074, Level 16, State 1, Line 2 The object 'DF__CompanyTr__Creat__0CDAE408' is dependent on column 'Created'. Msg 4922, Level 16, State 9, Line 2 ALTER TABLE DROP COLUMN Created failed because one or more objects access this column.

This is a code first table. Somehow the migrations have become all messed up and I am trying to manually roll back some changed.

I have no idea what this is:

DF__CompanyTr__Creat__0CDAE408

Solution

  • You must remove the constraints from the column before removing the column. The name you are referencing is a default constraint.

    e.g.

    alter table CompanyTransactions drop constraint [df__CompanyTr__Creat__0cdae408];
    alter table CompanyTransactions drop column [Created];