0

i'm new to Sqlite and EF.

i created a DbContext with some model classes which will create a local database on app start. Database will get updated while application running.

When i change something in model (ex: add new property to a class) and run the application, the whole database is recreating from scratch. so i'm loosing my saved data.

How can i keep the old data even though the model changes.

For Ex:

Database.table1
ID | Name
1  | abc
2  | xyz

when i add new prop to table1 class, its recreating table with empty data, where i need something like this if possible.

Database.table1
ID | Name | NewProp
1  | abc  | Null
2  | xyz  | Null
Ja9ad335h
  • 4,995
  • 2
  • 21
  • 29

1 Answers1

1

The database initializer is what causes the database to be recreated on a model change. So you can disable the initializer by setting it to null:

Database.SetInitializer<MyContext>(null);

This of course means you will need to manually make your database match the model. Another option is to use an initializer that doesn't drop and create the database. Looks like there is a project here for SqlLite: https://github.com/msallin/SQLiteCodeFirst So then you could use SqliteCreateDatabaseIfNotExists.

Finally, you could use migrations. Doesn't look like that's supported out of the box so you would need a third party tool for that. Entity Framework MigrationSqlGenerator for SQLite

Community
  • 1
  • 1
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • Thank you for the detailed answer. so i'm trying to create custom initializer for migration using `SQLite.CodeFirst` source code. If it doesn't turn well, i move to SQL CE. – Ja9ad335h Jan 22 '16 at 14:39