Skip to content

Creating Tables

Dominic Fischer edited this page Jul 21, 2017 · 2 revisions

Creation of tables is quite straight forward.
To create tables, use the CreateTable method in the SQLiteDatabase class.

For each model defined earlier, there will be a corresponding CreateTable method call.

The first argument is the name of the table. The name can have spaces.
The second argument is a lambda returning an anonymous class instance describing the column layout of the table. The optional third argument is another lambda, this time descibing the table constraints such as PrimaryKey, ForeignKey and Unique. One or more columns can be specified for these constraints.

CreateTable("Album", c => new
{
    AlbumId = c.Column<int>(primaryKey:true),
    Title = c.Column<string>(nullable:false),
    ArtistId = c.Column<int>()
},
t => new
{
    FK = t.ForeignKey(a => a.ArtistId, "Artist", "ArtistId")
});

CreateTable("Artist", c => new
{
    ArtistId = c.Column<int>(primaryKey: true),
    Name = c.Column<string>(nullable: false)
});

CreateTable("Customer", c => new
{
    CustomerId = c.Column<int>(primaryKey:true),
    FirstName = c.Column<string>(nullable:false),
    LastName = c.Column<string>(nullable: false),
    Company = c.Column<string>(),
    Address = c.Column<string>(),
    City = c.Column<string>(),
    State = c.Column<string>(),
    Country = c.Column<string>(),
    PostalCode = c.Column<string>(),
    Phone = c.Column<string>(),
    Fax = c.Column<string>(),
    Email = c.Column<string>(nullable:false),
    SupportRepId = c.Column<int?>()
},
t => new
{
    FK = t.ForeignKey(c => c.SupportRepId, "Employee", "EmployeeId")
});

CreateTable("Employee", c => new
{
    EmployeeId = c.Column<int>(primaryKey: true),
    FirstName = c.Column<string>(nullable: false),
    LastName = c.Column<string>(nullable: false),
    Title = c.Column<string>(),
    ReportsTo = c.Column<int>(),
    BirthDate = c.Column<DateTime?>(),
    HireDate = c.Column<DateTime?>(),
    Address = c.Column<string>(),
    City = c.Column<string>(),
    State = c.Column<string>(),
    Country = c.Column<string>(),
    PostalCode = c.Column<string>(),
    Phone = c.Column<string>(),
    Fax = c.Column<string>(),
    Email = c.Column<string>(nullable: false)
},
t => new
{
    FK = t.ForeignKey(c => c.ReportsTo, "Employee", "EmployeeId")
});

CreateTable("Genre", c => new
{
    GenreId = c.Column<int>(primaryKey: true),
    Name = c.Column<string>()
});

CreateTable("Invoice", c => new
{
    InvoiceId = c.Column<int>(primaryKey: true),
    CustomerId = c.Column<int>(),
    InvoiceDate = c.Column<DateTime>(),
    BillingAddress = c.Column<string>(),
    BillingCity = c.Column<string>(),
    BillingState = c.Column<string>(),
    BillingCountry = c.Column<string>(),
    BillingPostalCode = c.Column<string>(),
    Total = c.Column<decimal>()
},
t => new
{
    FK = t.ForeignKey(c => c.CustomerId, "Customer", "CustomerId")
});

CreateTable("InvoiceLine", c => new
{
    InvoiceLineId = c.Column<int>(primaryKey:true),
    InvoiceId = c.Column<int>(),
    TrackId = c.Column<int>(),
    UnitPrice = c.Column<decimal>(),
    Quantity = c.Column<int>()
},
t => new
{
    FK_Invoice = t.ForeignKey(i => i.InvoiceId, "Invoice", "InvoiceId"),
    FK_Track = t.ForeignKey(i => i.TrackId, "Track", "TrackId")
});

CreateTable("MediaType", c => new
{
    MediaTypeId = c.Column<int>(primaryKey: true),
    Name = c.Column<string>()
});

CreateTable("Playlist", c => new
{
    PlaylistId = c.Column<int>(primaryKey: true),
    Name = c.Column<string>()
});

CreateTable("PlaylistTrack", c => new
{
    PlaylistId = c.Column<int>(),
    TrackId = c.Column<int>()
},
t => new
{
    FK_Playlist = t.ForeignKey(pt => pt.PlaylistId, "Playlist", "PlaylistId"),
    FK_Track = t.ForeignKey(pt => pt.TrackId, "Track", "TrackId")
});

CreateTable("Track", c => new
{
    TrackId = c.Column<int>(primaryKey:true),
    Name = c.Column<string>(nullable:false),
    AlbumId = c.Column<int?>(),
    MediaTypeId = c.Column<int>(),
    GenreId = c.Column<int?>(),
    Composer = c.Column<string>(),
    Milliseconds = c.Column<int>(),
    Bytes = c.Column<int?>(),
    UnitPrice = c.Column<decimal>()
},
t => new
{
    FK_Album = t.ForeignKey(c => c.AlbumId, "Album", "AlbumId"),
    FK_MediaType = t.ForeignKey(c => c.MediaTypeId, "MediaType", "MediaTypeId"),
    FK_Genre = t.ForeignKey(c => c.GenreId, "Genre", "GenreId")
});

Just in case you were wondering. I didn't use the model classes to descibe table creation because of migrations. Basing table creations on classes could make future migrations difficult.
For simple databases it will not be an issue but if there is demand for it, then I will make methods for it.

In the table contraints argument, the name of the property will be the use to name the contraints in the CREATE statement. This is to make future migrations much easier.

Clone this wiki locally