Alan2
Alan2

Reputation: 24572

How can I use SQLite to create an index with a create table command?

I am using this code to create a table:

db2.CreateTable<Phrase>();

Here's the Phrase class:

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; }
}

When I hover over the CreateTable it gives a message saying that it creates any specified indexes on the columns of the table. It uses a schema automatically generated from the specified type.

Does anyone know how I could for example create an index on the PhraseNum?

Upvotes: 0

Views: 145

Answers (2)

SushiHangover
SushiHangover

Reputation: 74144

For a single property-based index add an IndexedAttribute:

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

For a multiple field index, use the Name and Order parameters within the IndexedAttribute:

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; }
    ~~~

Upvotes: 1

Mate
Mate

Reputation: 5274

Try

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

Upvotes: 1

Related Questions