Saving class schemes and tables when using ORM

I did not have much experience with ORM. I am trying to find a better way to handle adding / removing properties from a mapped class, ideally in an automatic and general way.

I am using ServiceStack.OrmLite and SQLite.

The purpose of the database is a document repository. There are no external problems with keys / referential integrity.

eg.

public class Foo { public Bar {get; set;} public Baz {get; set;} } 

Creates a Foo table using the columns Bar, Baz.

In an ideal world, the Foo class never changed, so I would not have to worry about it, but in fact it could be.

Let's say that Foo also needs the Quz property.

 public class Foo { public Bar {get; set;} public Baz {get; set;} public Quz {get; set;} } 

Reading is ok, Quz just returns as null, but insertion fails because there is no Quz column. It makes sense.

Let's say that Baz is no longer required.

 public class Foo { public Bar {get; set;} public Quz {get; set;} } 

Reading / pasting works just fine, however there will still be a Baz column in the base table, which is a bit confusing.

Given that this is a server application and will not restart frequently, a few things I considered and rejected:

  • At the beginning. If the Foo table exists, read it in temporary storage, release and recreate Foo (which means the table will be created with the correct schema), and then reinsert the data. This seems to work fine and synchronizes the circuit with the class, however it is too slow.

  • First, ask the base table to get the column definitions and use reflection to determine what the columns should be for the Foo class. Add / lower columns if necessary. I am pretty sure that I can make this approach work, but it is more complex than ideal, and I'm not sure if this is a good idea.

+4
source share

Source: https://habr.com/ru/post/1444505/


All Articles