Skip to content

Create Table

jgiacomini edited this page Oct 2, 2017 · 11 revisions

Create Table

Create Table is easy withTinySQLite

The first thing you have to do is to create a POCO class which represent your table :

public class MyTable
{
    public string MyColumn1 { get; set; }
    public string MyColumn2 { get; set; }
}

And for create table you have just to call CreateAsync method :

var context = new DbContext(_pathOfDb);
var table = context.Table<MyTable>();
await table.CreateAsync();

Create a column with PRIMARY KEY

You have to put the attribute [PrimaryKey] on the column you want to have a PK.

public class PrimaryTable
{
     [PrimaryKey]
     public int Primary { get; set; }
}

Multiple primary key is allowed.

public class DoublePrimaryKey
{
    [PrimaryKey]
    public int Primary1 { get; set; }

    [PrimaryKey]
    public int Primary2 { get; set; }
}

Create an AUTO INCREMENT column

The auto-incremented is only allowed on primary key column.

In SQLite allow only one auto-incremented column for each table.

public class MyTable
{
   [PrimaryKey(AutoIncrement =  true)]

   public int AutoInc { get; set; }
}

Ignore a proprerty

If you want to ignore of your POCO class you have to apply the [Ignore] on the property.

public class TableWithIngoredColumn
{
    [Ignore]
    public int IgnoredColumn{ get; set; }
}

The property without getter and setter are automatically ignored.

Clone this wiki locally