问题
i am using SQLite for Windows Runtime but i dont see any way to define relationships between tables. Is this feature supported in SQLite for WinRT?
If not, are there any alternative solutions for relational local database for Windows RT?
回答1:
The database engine itself supports relationships. One way is to specify the foreign keys when creating the tables:
CREATE TABLE Foo(
Id integer primary key autoincrement not null ,
...
)
CREATE TABLE Bar(
Id integer primary key autoincrement not null,
FooId integer not null REFERENCES Foo (Id) ON UPDATE CASCADE,
...
)
I believe each db connection has to also enable foreign keys by executing the following pragma:
PRAGMA foreign_keys = ON
In terms of the available ORMs, I guess that depends. I have only used SQLite-Net, so I can only speak about that. As far as I know, foreign keys are currently not a supported feature for SQL-Net. You have to manually manage the relationships.
For example, lets say your Foo
object has a reference in it to a Bar
object.
For creating the tables, you cannot use the SQLite-Net table creation methods. You have to use SQL to create the tables along with the relationships.
Also, when you use SQL-Net to load the Foo
object, the related Bar
object will not automatically be loaded. You will need to make a second SQL-Net call to load the Bar
object.
来源:https://stackoverflow.com/questions/16539683/relational-sqlite-on-windows-rt