0

我创建了一个简单的项目。添加了 Sqlite 和 Sqlite 扩展。但是,当我创建表时。Primary 和 ForeignKey 关系均未在表上建立。有人可以让我知道我做错了什么。

        SQLiteConnection connection = new SQLiteConnection("me4.db", SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite);
        connection.Execute("PRAGMA foreign_keys = ON");
        connection.Commit();

        connection.CreateTable<Group>();
        connection.CreateTable<UserGroup>();

在此处输入图像描述

[Table("group")]
public class Group
{
    public string DisplayName { get; set; }

    public string Descripton { get; set; }

    public bool IsPublic { get; set; }

    public string SmtpAddress { get; set; }

    [PrimaryKey]
    public string Id { get; set; }

}

[Table("usergroup")]
public class UserGroup
{
    public bool IsFavorite{ get; set; }

    public string LastVisitedTime { get; set; }

    [ForeignKey(typeof(Group),Unique=true)]
    public string Id { get; set; }
}
4

1 回答 1

0

SQLite.Net 不支持外键,SQLite-Net 扩展使用底层Indexed属性。

主键按预期工作,不确定为什么您的数据库浏览器没有显示它。

要检查它,您可以尝试以下代码:

var group = new Group { Id = 1 };
connection.Insert(group); // Element inserted
connection.Insert(group); // ERROR: constrain violation
于 2015-12-07T10:10:52.473 回答