通过ServiceStack.ORMLite调用sqlite函数

本文关键字:sqlite 函数 调用 ORMLite ServiceStack 通过 | 更新日期: 2023-09-27 18:16:30

我正在使用ServiceStack。ORMLite和SQLite作为数据库。我已经创建了一个通用存储库:

public class Repository<T> : IRepository<T> where T : class, new()
{
    private ReestrContext db;
    public Repository(ReestrContext db)
    {
        this.db = db;
    }
    public long CountAll()
    {
        return db.Connection.Count<T>();
    }
    public IQueryable<T> GetAll()
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit()).AsQueryable();
    }
    public IQueryable<T> GetAll(int rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit(rows)).AsQueryable();
    }
    public IQueryable<T> GetAll(int? skip, int? rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit(skip, rows)).AsQueryable();
    }
    public T GetById(long id)
    {
        return db.Connection.LoadSingleById<T>(id);
    }
    public long CountByCondition(Expression<Func<T, bool>> predicate)
    {
        long res = db.Connection.Count<T>(predicate);
        string qry = db.Connection.GetLastSql();
        return db.Connection.Count(predicate);
    }
    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit()).AsQueryable();
    }
    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(rows)).AsQueryable();
    }
    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int? skip, int? rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(skip, rows)).AsQueryable();
    }
    public long Create(T item)
    {
        using (var trans = db.Transaction)
        {
            long res = db.Connection.Insert(item, selectIdentity: true);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return res;
        }
    }
    public T Update(T item)
    {
        using (var trans = db.Transaction)
        {
            db.Connection.Update(item);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return item;
        }
    }
    public long Delete(long id)
    {
        using (var trans = db.Transaction)
        {
            long res = db.Connection.Delete(id);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return res;
        }
    }
}

在客户端,我创建了返回表达式树的过滤器函数。但是POCO类有

[Alias("Bd")]
[DataType(DataType.Date)]
public Nullable<DateTime> BirthdaySingle { get; set; }

字段,该字段也在过滤条件中使用。所以,我找不到在这个字段上正确创建过滤器的解决方案(因为表达式树不处理它),我想知道实现这种过滤的另一种解决方案是什么。ORMLite支持调用SQLite函数吗?在我的情况下,它需要是"日期"功能。或者它可能使用System.ComponentModel.DataAnnotations命名空间在字符串字段上设置[DataType(DataType.Date)]属性。我不知道。

通过ServiceStack.ORMLite调用sqlite函数

看起来你的代码引用了LINQ的Expression<T>树而不是OrmLite的SqlExpression<T>树。它们看起来很相似,但是OrmLite只支持将SqlExpression<T> lambda转换为查询。

我建议使用新发布的OrmLite Gistlyn沙盒来快速测试您的ORM代码。