SQLite File version compatibility within an application

痞子三分冷 提交于 2020-01-02 11:21:49

问题


I have a C# .NET application (kind of complex calculation app) in which the user inputs data and the processed information is saved into SQLite file using JSON serialization and EF. The same can be loaded into our application when required.

The application undergoes lot of changes during development and the classes are also modified. So the previously saved serialized objects in SQLite file differs from the newer one.

i want to provide a compatibilty for the old files so that it can be opened in the new application.

In simple, a .NET class "class XYZ" with bool as a member is serialized using JSON and saved as BLOB in a SQLite file. Later in the newer version of app, the bool member of the "class XYZ" is modifed as int. how to deserialize the previously saved file with bool type into the new app?

There are changes in the SQLite table structure as well across the version. how to achieve this in .NET?


回答1:


This is a pretty common problem, not bound to C# language I think. It've seen two methods of achieving such a versioning:

  1. Create a table with only one INTEGER column which holds only one row (thus simulating a database variable) containing version of both the database and structure schema. With each schema change you increase the version associated with it. For each new version you write upgrade statements and functions fetching raw JSON objects (not converted to class instances since they won't be compatible), updating, deleting and creating changed structure fields. Then you write converted structures to the upgraded schema. I've done it multiple times and sure, it quite tedious and you have to be meticulous, but that's pretty much the only reliable way of doing that (that I'm aware of).
  2. Use a library such as Protocol Buffer which has support for structure changes (limited, of course). Such a library can assist you in structure changes, but not in database schema changes - either way you will probably have to maintain mentioned one-cell table with schema version and upgrade it whenever a lower number is seen.


来源:https://stackoverflow.com/questions/13720206/sqlite-file-version-compatibility-within-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!