question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migration tries to create existing table

See original GitHub issue

Dear @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:open
  • Created 3 months ago
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
digocesarcommented, Jul 6, 2023

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

0reactions
Tahirhancommented, Jul 6, 2023

Hmm, this solution can be implemented. Thanks @digocesar!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET Core Update migration is trying to re-create table again
My migration is trying to create new table in my database. But in migration there is nothing for this. Here is my migration...
Read more >
Subsequent migrations re-creates table that is already ...
EF Core - Migration - Subsequent migrations re-creates table that is already created by another migration (Foreign Key) #14283.
Read more >
Code First Migrations with an existing database - EF6
We're going to generate an InitialCreate migration that includes logic to create the existing schema.
Read more >
Database already exists when running EF Core migrations
It shouldn't be trying to create a new database at all… right? It turns out that the database exists, but the user account...
Read more >
Entity Framework Core Migrations
The migrations feature in Entity Framework Core enables you to make changes to your model and then propagate those changes to your database ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found