0

I created a simple project. Aded the Sqlite and Sqlite extensons. However when I create the tables. Neither Primary nor ForeignKey relation is established on the table. Could someone let me know what am I doing wrong.

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

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

enter image description here

[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; }
}
imasud
  • 171
  • 2
  • 9

1 Answers1

0

SQLite.Net doesn't have support for Foreign Keys, SQLite-Net Extensions uses Indexed properties under the hood.

Primary Keys work as expected, not sure why your database browser is not showing it.

To check it, you can try this code:

var group = new Group { Id = 1 };
connection.Insert(group); // Element inserted
connection.Insert(group); // ERROR: constrain violation
redent84
  • 18,901
  • 4
  • 62
  • 85