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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 10:23:06

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/14104031/how-to-map-sqlite-tables-and-columns-to-c-sharp-classes-in-windows-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!