我正在研究 SQLite - .net ( https://github.com/praeclarum/sqlite-net ),以便将本地存储添加到我的项目中。
我提出了几个问题:
表是按模型 创建的,然后为了创建表,应使用以下代码:
var db = new SQLiteConnection("foofoo"); db.CreateTable<Stock>(); db.CreateTable<Valuation>();
问题是,在 MVVM 方法中,它应该写在哪里? 我想到了 bootstrapper.cs,但我不太确定它的效果如何。
在其中一个示例中,这是某个模型的示例代码:
public class Valuation { [PrimaryKey, AutoIncrement] public int Id { get; set; } [Indexed] public int StockId { get; set; } public DateTime Time { get; set; } public decimal Price { get; set; } }
[Indexed]
语句是仅应用于StockId
,还是应用于所有三个StockId
,时间
和价格
?
最佳答案
数据服务!只需创建一个 DataService 类。将它传递给数据库的路径或查找它的配置。让构造函数创建 SQLiteConnection,创建表,添加任何默认数据。
如果您使用的是 IOC 容器,则可以注册 DataService 以在您的 ViewModel 中使用。考虑将 create immediate 标志设置为 True 进行注册,以便立即初始化数据库。
抱歉,我目前没有可用的示例。
关于c# - 使用 SQLite-net 库创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16538747/