如何使用SQLite通过create table命令创建索引?

时间:2018-09-01 07:05:42

标签: sqlite xamarin xamarin.forms

我正在使用以下代码创建表:

db2.CreateTable<Phrase>();

这是词组类:

public class Phrase : IPhrase
{
    [PrimaryKey, NotNull]
    public string PhraseId { get; set; }
    public int PhraseNum { get; set; }
    public int CategoryId { get; set; }
    public string English { get; set; }
    public string Romaji { get; set; }
}

当我将鼠标悬停在CreateTable上时,它会显示一条消息,指出它将在表的列上创建任何指定的索引。它使用从指定类型自动生成的架构。

有人知道我如何在PhraseNum上创建索引吗?

2 个答案:

答案 0 :(得分:1)

尝试

  [Indexed]
  public int PhraseNum { get; set; }

答案 1 :(得分:1)

对于单个基于属性的索引,请添加IndexedAttribute

public class Package
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Indexed]
    public string Name { get; set; }
    ~~~~

对于多字段索引,请使用IndexedAttribute中的Name和Order参数:

public class Package
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Indexed(Name = "SecondaryIndex", Order = 1)]
    public string Name { get; set; }
    [Indexed(Name = "SecondaryIndex", Order = 2)]
    public string PackageName { get; set; }
    ~~~