codemonkey85 / BlazorWasmSQLiteTest

Testing client-side SQLite in Blazor WebAssembly
1 stars 1 forks source link

adding new properties to ToDo class generate EF Core exception #1

Open odyyudah opened 2 years ago

odyyudah commented 2 years ago

Added new properties to ToDo class and `public class Todo { [Key] public int Id { get; set; }

    public string Title { get; set; }

    public bool IsComplete { get; set; }

    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
    public string Property5 { get; set; }
    public string Property6 { get; set; }
}`

And modified ClientSideDbContext.cs `protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Todo>().HasData(new Todo[]
        {
            new Todo { Id = 1, Title = "Todo 1", IsComplete = false, Property1 = "p1", Property2 = "p2", Property3 = "p3", Property4 = "p4", Property5 = "p5", Property6 = "p6"},
            new Todo { Id = 2, Title = "Todo 2", IsComplete = false, Property1 = "p1", Property2 = "p2", Property3 = "p3", Property4 = "p4", Property5 = "p5", Property6 = "p6" },
            new Todo { Id = 3, Title = "Todo 3", IsComplete = false, Property1 = "p1", Property2 = "p2", Property3 = "p3", Property4 = "p4", Property5 = "p5", Property6 = "p6" },
        });
    }`

The Exception is: An exception occurred while iterating over the results of a query for context type 'BlazorWasmSQLiteTest.ClientSideDbContext'. Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such column: t.Property1'. at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext() at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext() at Microsoft.Data.Sqlite.SqliteDataReader.NextResult() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)

I have to manually delete the IndexedDB.SqliteStorage using F12 then reload the page, only then application is running fine and no more EF exception. please advise, why does this happen whenever I added new properties?

thank you

codemonkey85 commented 2 years ago

Since the schema is changing on the client side but not on the DB side, there is a mismatch and EF Core tries to query a column that doesn't exist in the DB yet. Either the DB has to be manually updated to add the new tables / columns, or we have to use migrations: https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli (although I'm not sure migrations will work with IndexedDB.SqliteStorage).