在Windows Phone 8.1的sqlite中进行泛化CRUD操作

时间:2015-03-18 12:00:15

标签: c# sqlite windows-phone-8 windows-phone-8.1 winrt-xaml

我想在我的应用程序中进行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();

现在只针对单个表我想为所有表执行此操作我该怎么做才需要帮助。 感谢。

1 个答案:

答案 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