Need A Reliable Software Technology Partner?
00D
00H
00M
00S
Explore Now
← Back to Articles
Development / Web & Mobile Development

Asp.net core mvc code first approach migration issues & fixes| failed executing DbCommand| LTER TABLE DROP COLUMN failed | Cannot find the object “tablename” because it does not exist

PPrem2 Min Read
Asp.net core mvc code first approach migration issues & fixes| failed executing DbCommand| LTER TABLE DROP COLUMN failed | Cannot find the object “tablename” because it does not exist

Failed executing DbCommand (68ms) [Parameters=[], CommandType=’Text’, CommandTimeout=’30’]

DECLARE @var0 sysname;

SELECT @var0 = [d].[name]

FROM [sys].[default_constraints] [d]

INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]

WHERE ([d].[parent_object_id] = OBJECT_ID(N'[tblOrders]’) AND [c].[name] = N’Id’);

IF @var0 IS NOT NULL EXEC(N’ALTER TABLE [tblOrders] DROP CONSTRAINT [‘ + @var0 + ‘];’);

ALTER TABLE [tblOrders] DROP COLUMN [Id];

Microsoft.Data.SqlClient.SqlException (0x80131904): ALTER TABLE DROP COLUMN failed because column ‘Id’ does not exist in table ‘tblOrders’.

ALTER TABLE DROP COLUMN failed because column ‘Id’ does not exist in table ‘tblOrders’.

Save Time, Avoid Errors & Ensure Smooth Database Deployments

Solutions:

Generally, these issues coming on code first migration when you are not defining the relations and later modify it and run migration alteration do not happen.

Steps to solve this kind of issue on DB migrations:

  1. Remove the last table migration in my case it is “tblOrder”.
  2. Aslo remove table model entity creations and entity table navigation property from “ApplicationDbContextModelSnapshot”
  3. Next delete the table “tblOrder” if you have foriegnkey in it, you will not able to delete may give you error like:

The DELETE statement conflicted with the REFERENCE constraint, so delete all the related table as well.

4. now add db migration by running command in

5. package manager console:> EntityFrameworkCore\Add-Migration commetnsrelatedtable

6. package manager console:> EntityFrameworkCore\Update-Database

It should resolve your problem,

Thanks for visiting this article, if not resolve don’t hesitate to put comments.

Frequently Asked Questions

What is Asp.net core mvc code first approach migration issues & fixes| failed executing DbCommand| LTER TABLE DROP COLUMN failed | Cannot find the object “tablename” because it does not exist?

Facing issues with Code First migrations in ASP.NET Core MVC? Fix common Entity Framework errors like "Cannot find the object 'tablename'" or "ALTER TABLE DROP COLUMN failed" with proven solutions.

Reader Responses

Join the Conversation