Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite does not support this migration operation ('DropForeignKeyOperation')

Tags:

I'm building a web API using Asp.Net core 3.0 with EFcore. I'm using code first approach. I did my initial migration with this model

  public class Group {
    [Key]
    public int Id{get;set;}
    public string Title { get; set; }
   }

Later I added a foreign key relationship to this model

  public class Group {
    [Key]
    public int Id{get;set;}
    public string Title { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }
    public int UserId { get; set; }
   }

The new migrations generates properly when I run Add-Migration <migrationName> on PMC. But when I run update-database I get this error: SQLite does not support this migration operation ('DropForeignKeyOperation')

like image 607
Ndubuisi Jr Avatar asked Apr 04 '20 17:04

Ndubuisi Jr


1 Answers

This is a documented limitation of the SQLite Database Provider:

Workaround (Source: MSDN)

You can workaround some of these limitations by manually writing code in your migrations to perform a table rebuild. A table rebuild involves renaming the existing table, creating a new table, copying data to the new table, and dropping the old table. You will need to use the Sql(string) method to perform some of these steps.

Edit: As directed by @Shawn it seems MSDN is out of date and this is a malpractice. Instead use the Sql(string) method to issue an ALTER TABLE statement as documented here.

like image 150
simon-pearson Avatar answered Oct 11 '22 17:10

simon-pearson