如何在 Windows 8 中将 sqlite 表和列映射到 c# 类 [英] How to map sqlite tables and columns to c# classes in windows 8

查看:17
本文介绍了如何在 Windows 8 中将 sqlite 表和列映射到 c# 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 为 windows 8 构建我的第一个应用程序(我是一个完全的菜鸟).我已经创建了一些类.当应用程序运行时,它需要从 sqlite 数据库读取数据.我为每个表都有一个类,但我不知道如何将每个类属性映射到数据库中的相应列.我似乎找不到任何信息,而且我所看到的所有内容似乎都自动映射了它们,或者没有提及它是如何完成的,例如

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

}

样品

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

是否有我遗漏的内容或某种教程?我以前从未使用过 sqlite.

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

推荐答案

好吧,我觉得我很傻……原来你必须在类上设置表和列属性.例如

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

这篇关于如何在 Windows 8 中将 sqlite 表和列映射到 c# 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆