0
我使用ServiceStack.ORMLite和SQLite作为数据库。我创建了一个通用的存储库:通过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)]
属性。我不知道。请帮帮我。
感谢您的链接,我会尝试。但还有一个问题。 LINQ表达式工作正常,但如果使用DateTime字段则不适用。那么分层体系结构如何,我需要在表示层中引用OrmLite库吗?或者我需要在另一层中构建过滤器? – Dmitry