如何在使用sqlite-net的所有数据库模型的父类中实现插入,更新和删除?

时间:2013-04-12 21:36:06

标签: c# sqlite windows-store-apps sqlite-net

我在SQLite中使用Windows 8 Store Application数据库。 此外,我使用SQLite包装器sqlite-net对数据库进行操作。

我将详细解释这一点,以便您了解我的情况。 有Model classes包含业务逻辑,例如类位置。然后从DatabaseModels生成数据库表,例如 LocationDb

我不想实现基本数据库方法,如插入(模型模型),更新(模型模型)删除(int id)每个Model类中的GetAllRecords() GetRecord(int id)。相反,我想在所有模型的基类中实现这些方法。所有模型的基类都称为 ModelBase ;所有数据库模型的基类都是 DbModelBase

对于所有模型,如下所示实现Insert方法没有问题:

    public int Insert(ModelBase model)
    {
        int id;
        using (var db = new SQLite.SQLiteConnection(MainApplication.DBPath))
        {
            db.Insert(model.GetDbModelFromModel());
            id = (int)SQLite3.LastInsertRowid(db.Handle);
        }
        return id;
    }

但我不知道如何使用sqlite-net实现其他的。 我需要在特定的表中找到特定的数据记录。我有一个包含id的模型对象。 但是如何使一个方法适用于所有模型类而不显式调用特定的表? 以下代码适用于单个特定数据库表...

        using (var db = new SQLite.SQLiteConnection(MainApplication.DBPath))
        {
            var queryResult = db.Table<LocationDb>().Where(l => l.Id == model.Id);
            if (queryResult.Count() == 1)
            {
                db.Update(new TeacherDb(model));
            }
        }

......但我不能写

var queryResult = db.Table<typeof(databaseModel)>().Where(t => t.Id == model.Id);

2 个答案:

答案 0 :(得分:0)

为此写一个generic function

答案 1 :(得分:0)

这个解决方案怎么样:

public class BaseModel<T> where T : Type
{
    protected T Id;
    /// <summary>
    /// Adds a new record to data base. It doesn't check for record existance.
    /// </summary>
    /// <returns></returns>
    protected BaseModel<T> Add() 
    {
        try
        {
            var entities = Variable.CurrentEntities;
            var dbSet = entities.Set<BaseModel<T>>();
            var result = dbSet.Add(this);
            entities.SaveChanges();
            return result;
        }
        catch (Exception exception)
        {
            LogException.HandleException(exception);
            return null;
        }
    }

    protected bool Update()
    {
        try
        {
            var entities = Variable.CurrentEntities;
            var dbSet = entities.Set<BaseModel<T>>();
            var original = dbSet.Find(this);
            entities.Entry(original).CurrentValues.SetValues(this);
            entities.SaveChanges();
            return true;
        }
        catch (Exception exception)
        {
            LogException.HandleException(exception);
            return false;
        }
    }

    protected BaseModel<T> Delete()
    {
        try
        {
            var entities = Variable.CurrentEntities;
            var dbSet = entities.Set<BaseModel<T>>();
            var result = dbSet.Remove(this);
            entities.SaveChanges();
            return result;
        }
        catch (Exception exception)
        {
            LogException.HandleException(exception);
            return null;
        }
    }

    protected bool Upsert()
    {
        try
        {
            var entities = Variable.CurrentEntities;
            var dbSet = entities.Set<BaseModel<T>>();
            dbSet.AddOrUpdate(this);
            return true;
        }
        catch (Exception exception)
        {
            LogException.HandleException(exception);
            return false;
        }
    }

    protected BaseModel<T> Save()
    {
        try
        {
            var entities = Variable.CurrentEntities;
            var dbSet = entities.Set<BaseModel<T>>();
            var original = dbSet.Find(Id);
            if (original == null)
            {
                original = dbSet.Add(this);
            }
            else
            {
                entities.Entry(original).CurrentValues.SetValues(this);
            }
            entities.SaveChanges();
            return original;
        }
        catch (Exception exception)
        {
            LogException.HandleException(exception);
            return null;
        }
    }
}