ManyToOne constraint not working

牧云@^-^@ 提交于 2019-12-07 18:07:27

Yes, it is normal. SQLite-Net Extensions is built on top of SQLite-Net, that doesn't have support for foreign key constraints, so there's no foreign key declared at the database layer.

Using core SQLite-Net methods, the [ForeignKey] property is just an integer property that doesn't have any kind of restriction, so you can modify it at will without runtime errors.

If your project requires foreign key constraints at database layer, you could use Execute method to add the constraints to the columns that you want manually.

UPDATE:

Foreign keys are disabled by default in SQLite. To enable foreign keys in SQLite-Net, you have to add the following code to the beginning of your program. This statement is not persisted in the database and must be executed every time the application starts:

database.Execute("PRAGMA foreign_keys = ON");

To check that it's enabled, you can use PRAGMA foreign_keys:

int result = database.ExecuteScalar<int>("PRAGMA foreign_keys");
if (result == 1) {
    // Foreign keys enabled
}

After it's enabled, you can add foreign keys to your tables declarations (sqlite doesn't support adding constraints on ALTER statements) like this:

CREATE TABLE child ( 
    id           INTEGER PRIMARY KEY, 
    parent_id    INTEGER, 
    description  TEXT,
    FOREIGN KEY (parent_id) REFERENCES parent(id)
);

Obviously creating the tables manually forces you to not use SQLite-Net CreateTable statements.

Problem solved. I forgot to copy my database from Resources folder (as template) to app data folder, so technically my databse was being created by SQLite-Net, without any foreign keys. Thanks for help

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