Drop/create SQLite columns when changing column type to something not convertible

Open
#22,598 19 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sqlite
Domain
databases

Research direction

Reproduce the failure using the linked test project and the migration created after changing Post.Id from Guid to Int32. Start at the migration execution triggered by context.Database.Migrate() in Test/Program.cs line 14, then inspect the generated SQLite migration and its table-rebuild operations. Done means the migration handles a non-convertible column change without the datatype mismatch exception.

Written by the indexing model from the issue text.

Description

area-migrations area-sqlite customer-reported

Hello.
I have simple class library project with a EF Core 5.0-rc1 DbContext that targets .NET 5.0-preview7 (I'm using .NET 5-preview7 because according to info on download page it is the latest one that supports Visual Studio 2019 v16.7 Release. I use only stable IDE builds in my project and cannot use preview IDE versions).
I've have following entites

public class User
    {
        public Guid Id { get; set; }
        public String Name { get; set; }
        public Int32 Age { get; set; }

        public virtual IList<Post> Posts { get; set; }
    }

    public class Post
    {
        public Guid Id { get; set; }
        public String Text { get; set; }
        public DateTime PostDate { get; set; }
        public Guid UserId { get; set; }
        public virtual User User { get; set; }
    }

Then I've added first migration via dotnet ef migrations add InitialCreate command, created test database and populated some data.
After that I've changed Post.Id data type from Guid to Int32 and created second migration.

According to this SQLite migrations table rebuilds are now available since EF Core 5.0.0-preview8.
Also according to docs AlterColumn command should work for SQLite via table rebuild.

But running context.Database.Migrate(); throws an exception

SQLite Error 20: 'datatype mismatch'.'
Steps to reproduce
  1. Download test project - https://yadi.sk/d/iZiJnlidwyNMlg (test database test.db with some data is already placed in program output folder).
  2. Compile and run Test console application - context.Database.Migrate(); throws an exception
Exception StackTrace:
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Test.Program.Main(String[] args) in D:\Test\Test\Program.cs:line 14
Further technical details

EF Core version: 5.0-rc1
Database provider: Microsoft.EntityFrameworkCore.Sqlite 5.0-rc1
Target framework: .NET 5.0-preview7
Operating system: Windows 10 2004 x64
IDE: Visual Studio 2019 16.7.3 Professional

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.