EF Core Create a field that doesn't exist

c# entity-framework entity-framework-core sqlite

Question

I'm using Entity Framework Core - Code First to load data from a SQLite file. The problem is I updated the schema of the new SQLite DB files and added fields in a table. It works fine for the newly generated DB. but, when I try to open an old file, I got an exception. I don't really understand EF. I need help cause I have 2 questions :

  • Is it possible to avoid getting the exception if the field doesn't exist ? (If so, an example could be very nice)
  • If not, is it possible to auto update a SQLite file if the fields are missing ?

Thank you in advance.

Exception I got :

As you can see, the field COMMENT doesn't exist in table GAP.

SQLite Error 1: 'no such column: g.COMMENT'.
    à Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db)
    à Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
    à Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior)
    à System.Data.Common.DbCommand.ExecuteReader()
    à Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
    à Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
    à Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer)
    à Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](Func`2 operation, Func`2 verifySucceeded, TState state)
    à Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, TState state)
    à Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
    à Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
    à Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
    à     Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
    à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    à Dodo.GapTable.LoadData() dans d:\SYNC\DLSFile\Dodo\Table\GapTable.cs:ligne 35
1
0
9/10/2018 2:16:37 PM

Popular Answer

It sounds like you're running into a migration issue.

Your 'new' database that is working is probably using the most up-to-date migration.

When trying access an 'old' database file, you likely have not applied the migration to that file yet, and since your models have changed, they no longer match the schema of the database and will cause an error.

To complete a migration for your old databases, make sure the old database is targeted properly by your connection string in your DbContext class, and then run the command update-database (from the VS powershell) or dotnet ef database update --project {your-project-path} (from the command line)

Microsoft Docs : https://docs.microsoft.com/en-us/ef/core/managing-schemas/

0
9/10/2018 2:23:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow