是否可以在ServiceStack.OrmLite中加载多个嵌套对象

时间:2016-11-12 19:30:03

标签: c# sqlite ormlite-servicestack

我在我的项目中使用ServiceStack.OrmLite作为ORM而且我遇到了一个问题。我在SQLite数据库中有4个表:Person,Predmet(具有PersonId外键和两个字段引用Dic表:DicType和DicCode),Photo和Dic(包含具有Decode字段的分层字典值)。 所以,我的代码是:

[Alias("Person")]
public partial class Person : IHasId<long>
{
    [PrimaryKey, AutoIncrement]
    public long Id { get; set; }
    public string Name { get; set; }
    [Alias("Bd")]
    public Nullable<int> BirthdaySingle { get; set; }
    [Alias("Bp")]
    public string BirthPlace { get; set; }
    [Alias("Org")]
    public string Organization { get; set; }
    [Reference]
    public List<Subject> Subjects { get; set; }
    [Reference]
    public List<Photo> Photos { get; set; }
}

[Alias("Photo")]
public partial class Photo : IHasId<long>
{
    [PrimaryKey, AutoIncrement]
    public long Id { get; set; }
    [Alias("Ph")]
    public byte[] RealPhoto { get; set; }
    [Alias("IdxPh")]
    public byte[] IndexedPhoto { get; set; }
    [Alias("DateTaken")]
    public Nullable<DateTime> PhotoDate { get; set; }
    [References(typeof(Person))]
    public Nullable<long> PersonId { get; set; }
}

[Alias("Predmet")]
public partial class Subject : IHasId<int>
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Alias("PType")]
    [References(typeof(Dic))]
    public int ParentDicCode { get; set; }
    [Alias("PCode")]
    [References(typeof(Dic))]
    public int ChildDicCode { get; set; }
    [Alias("PValue")]
    public string Value { get; set; }
    [Alias("PDescription")]
    public string Description { get; set; }
    [References(typeof(Person))]
    public Nullable<long> PersonId { get; set; }
    [Reference]
    public Dic Parent { get; set; }
    [Reference]
    public Dic Child { get; set; }
}

[Alias("Dic")]
public partial class Dic : IHasId<int>
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int DicNum { get; set; }
    public int DicCode { get; set; }
    public string DicDecode { get; set; }
}

我决定创建通用存储库:

public class Repository<T> : IRepository<T> where T : class, new()
{
    // Contains IDbConnection initializer and code to open Transaction
    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)
    {
        var res = db.Connection.SelectLazy(db.Connection.From<T>().Limit(skip, 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)
    {
        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)
    {
        var res = db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(skip, 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;
        }
    }
}

我是否正确引用对象? 您能否帮助我正确组织Dic课程中嵌套Subject对象的加载...我非常感谢您的帮助。

0 个答案:

没有答案