Forums - C# Corner

Forum guidelines
Makinde A. Israel

Makinde A. Israel

  • 1.6k
  • 160
  • 7.4k

DbMigration: How to use IF EXISTS in SQL()

Mar 12 2019 3:25 AM
Please I have a web App with lots of migrations.I need to drop an index first if it exist before creating it.In the Dbmigration method. I usedDropIndex("[Log]", "[IX_ContentL]") But it throws error, the specified index does not exist.So I tried to first check if the index exist or not usingSql(" IF EXISTS(SELECT * FROM sys.indexes WHERE name=[IX_ContentL] AND object_Id = [dbo].[Log]) DROP INDEX ([IX_ContentL]);");But it throws an unable to parse statement error with the following information.[Token Line 1, Token offset 1, Token error IF]Please help me, I've been on this 3 days now. I've tried different combination as suggested on different blogs.It seems the Sql() function does not recognize the IF EXISTS statement.Please I need help.Thanks.

Answers (3)