简体   繁体   English

通过ServiceStack.ORMLite调用sqlite函数

[英]Calling sqlite function via ServiceStack.ORMLite

I'm using ServiceStack.ORMLite and SQLite as database. 我正在使用ServiceStack.ORMLite和SQLite作为数据库。 I've created a generic repository: 我创建了一个通用存储库:

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;
        }
    }
}

On a client side I created filter function that returns an expression tree. 在客户端,我创建了返回表达式树的过滤器函数。 But my POCO class has 但是我的POCO课有

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

field which also is using in filter conditon. 也用于过滤条件的字段。 So, I can't find the solution to correctly create filter on this field (because expression tree doesn't handle it) and I want to know what could be another solution to achieve such filtering. 因此,我找不到在此字段上正确创建过滤器的解决方案(因为表达式树无法处理它),并且我想知道实现这种过滤的另一种解决方案。 Does ORMLite support calling SQLite functions? ORMLite是否支持调用SQLite函数? In my case it needs to be "DATE" function. 就我而言,它需要是“ DATE”功能。 Or maybe it uses System.ComponentModel.DataAnnotations namespace to set [DataType(DataType.Date)] attribute on string field. 也许它使用System.ComponentModel.DataAnnotations命名空间在字符串字段上设置[DataType(DataType.Date)]属性。 I don't know. 我不知道。 Help me please. 请帮帮我。

It looks like your code has referenced LINQ's Expression<T> tree instead of OrmLite's SqlExpression<T> tree. 看起来您的代码已引用LINQ的Expression<T>树而不是OrmLite的SqlExpression<T>树。 They look similar, but OrmLite only has support to transform SqlExpression<T> lambdas into queries. 它们看起来很相似,但是OrmLite仅支持将SqlExpression<T> lambda转换为查询。

I would recommend playing around in the newly-released OrmLite Gistlyn sandbox to quickly test out your ORM code. 我建议您在新发行的OrmLite Gistlyn沙箱玩耍,以快速测试您的ORM代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM