TemiGiwa
TemiGiwa

Reputation: 411

How to map sqlite tables and columns to c# classes in windows 8

I'm building an my first app (I'm a complete noob) in C# for windows 8. And I have some classes that I have created. When the app runs it needs to read data from a sqlite database. I have a class for each table, but I don't know how to map each class property to its respective column in the database. I can't seem to find any info and all the stuff I've seen seems to map them automatically or doesn't mention how it's done e.g.

public class Person
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[MaxLength(30)]
public string Name { get; set; }

[MaxLength(30)]
public string Surname { get; set; }

}

Sample

var query = conn.Table<Person>().Where(x => x.Name == "Matteo");
var result = await query.ToListAsync();
foreach (var item in result)
{
    Debug.WriteLine(string.Format("{0}: {1} {2}", item.Id, item.Name, item.Surname));
}

Is there something I'm missing or a tutorial of some sort? I've never worked with sqlite before.

Upvotes: 4

Views: 10827

Answers (1)

TemiGiwa
TemiGiwa

Reputation: 411

Ok I feel foolish... it turns out you have to set the table and column attributes on the class. eg

[Table("sometable")]
class Element
{
    [Column(Name = "id")]
    public int Id { get; set; }

    [Column("columnone")]
    public string PropertyOne {get; set;}

    [Column("columntwo")]
    public string PropertyTwo {get; set;}
}

Upvotes: 10

Related Questions