私は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