开发者

SubSonic and Tree table structure

I have to store a tree-like structure (think folders, for instance) in my database. The model I chose is pretty simple: the table has a FolderId (PK, int, identity), some random attributes and a null开发者_如何学Goable ParentId (same-table-FK to FolderId, int, nullable).

It all works great and all. I'm using SubSonic's ActiveRecord template. Is there any way to be able to have my generated Folder class have Parent / Children attributes, instead of simply, "Folders"?

I suspect I have to edit the template, probably ActiveRecord.tt. If so, could someone point me to a starting point? Maybe someone has done something similar?


I guess it was too late to see the obvious: I can simply do something along the lines of

partial class Folder
{
    public Folder Parent
    {
        get {
            if (_ParentId.HasValue)
            {
                var repo = ACME.Folder.GetRepo();
                return
                    (from items in repo.GetAll()
                     where _ParentId.Value == items._FolderId
                     select items).First();
            }
            else 
                return null;
        }
    }

    public IQueryable<Folder> Children
    {
        get {
            var repo = ACME.Folder.GetRepo();
            return from items in repo.GetAll()
                   where _FolderId == items.ParentId
                   select items;
        }
    }
}


All of the generated classes are Partials so you can do what you did above, but simply create a partial class for it so it's all in the "same" class...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜