我想在我的应用程序中进行crud操作操作。我有50张桌子。我想在所有表上应用插入,更新,删除。那么我可以为每个表进行所有4个操作,或者我将执行一个将执行CRUD的generaliza 4函数。
例如我以这种方式为
做using (var db = new SQLite.SQLiteConnection(dbpath))
{
foreach (var item in temp.Result.StaticTable)
{
db.Insert(new StaticTable()
{
siStaticTableID = item.siStaticTableID,
vcTableName = item.vcTableName,
dtUpdTime = item.dtUpdTime
});
}
db.Commit();
db.Dispose();
db.Close();
var line = new MessageDialog("Records Inserted");
await line.ShowAsync();
现在只针对单个表我想为所有表执行此操作我该怎么做才需要帮助。 感谢。
答案 0 :(得分:0)
看看这个关于使用实体框架的通用存储库的示例。
尝试实现类似这样的东西
您的基本型号
class BaseModel {
//TO ensure all your models have and id
[SQLite.PrimaryKey]
public int Id { get; set; }
}
你的事实模型
class Customer :BaseModel
{
public string Name { get; set; }
public string City { get; set; }
public string Contact { get; set; }
}
基础Crud
class BaseCrud<T> where T : BaseModel {
//initialice your db
protected SQLite.SQLiteConnection db;
public bool Delete(T item) {
return db.Delete<T>(item.Id) > 0;
}
public bool Update(T item) {
return db.Update(item) > 0;
}
public bool Insert(T item) {
return db.Insert(item) > 0;
}
}
如果您需要在模型上做一些特殊的事情
,那就是特定的问题class CustomerCrud : BaseCrud<Customer> {
public List<Customer> GetAll() {
return db.Table<Customer>().ToList();
}
}
Generic repository Entity Framework
And this another tutorial about SQLite
另请查看repository pattern。