Migration tries to create existing table
See original GitHub issueDear @msallin , thank you for your work. I am trying to implement migration branch into my project. I have added a new column to my existing table and used “SqliteMigrateDatabaseToLatestVersion” as an initializer. But when I use the DbContext I get an error like this:
System.Data.SQLite.SQLiteException: 'SQL logic error table Blocks already exists'
As I understand, MigrationBuilder tries to create an existing table although it has no change. I have only added one column to the “Parts” table.
Here is my DbContext class:
namespace MyProject.Model.Context
{
public class MyProjectLANDb : DbContext
{
public MyProjectLANDb() : base(new SQLiteConnection("Data Source=" + Properties.Settings.Default.LANDbPath + "\\MyProjectLAN.sqlite;Persist Security Info=False;"), contextOwnsConnection: true)
{
Configure();
}
private void Configure()
{
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteMigrateDatabaseToLatestVersion<MyProjectLANDb, Migrations.Configuration>(modelBuilder, true);
Database.SetInitializer(sqliteConnectionInitializer);
}
public DbSet<Block> Blocks { get; set; }
public DbSet<Part> Parts { get; set; }
}
}
And Migrations Configuration:
namespace MyProject.Model.Migrations
{
internal sealed class Configuration : DbMigrationsConfiguration<MyProjectLANDb>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
// This command alter the class to support Migration to SQLite.
SetSqlGenerator("System.Data.SQLite", new SqliteMigrationSqlGenerator());
}
protected override void Seed(MyProjectLANDb context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
SQLite.CodeFirst reference runtime version: v4.0.30319 .NET Framework version: 4.8 Entity Framework version: 6.4.4
What I am missing? I just want to make the database updated with added column, Thanks!
Issue Analytics
- State:
- Created 3 months ago
- Comments:17 (2 by maintainers)
Only recently after version 3.35.0 of Sqlite it became possible to do DROP COLUMN. And even then, Sqlite actually just hides the column, without physically removing it from the disk. More information: https://stackoverflow.com/questions/8442147/how-to-delete-or-add-column-in-sqlite
Maybe the componente should validade the Sqlite version in this statement generation.
But if you have the component code in your project you can try to implement this code in the drop column function: https://github.com/msallin/SQLiteCodeFirst/blob/3816796944c3a194890bdc3c8c358825560a77b9/SQLite.CodeFirst/Internal/Builder/MigrationBuilder.cs#L231
Hmm, this solution can be implemented. Thanks @digocesar!