Castle ActiveRecord学习

Posted 邢帅杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Castle ActiveRecord学习相关的知识,希望对你有一定的参考价值。

一、延迟加载

//用户发布的主题,一对多;Table:外键表;ColumnKey:外键;Lazy:延迟加载;Cascade:级联操作(级联删除)
        [HasMany(typeof(ThemeInfo), Table = "ThemeInfo", ColumnKey = "UserID", Lazy = true, Cascade = ManyRelationCascadeEnum.Delete)]
        public virtual IList<ThemeInfo> ThemeInfos { get; set; }

Lazy=true,当需要用到的时候才会自动读取,用法:

//延迟加载,必须使用 using (new SessionScope())
            using (new SessionScope())
            {
                StringBuilder htmlStr1 = new StringBuilder();
                IList<Models.UserInfo> list1 = Models.UserInfo.FindAll();
                foreach (Models.UserInfo item in list1)
                {
                    htmlStr1.Append("编号:" + item.ID + "  名称:" + item.Name + "<br />");
                    htmlStr1.Append("&nbsp;&nbsp;&nbsp;&nbsp;我的主题:<br />");
                    foreach (Models.ThemeInfo item2 in item.ThemeInfos)
                    {
                        htmlStr1.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号:" + item2.ID + "  名称:" + item2.Subject + "<br />");
                    }
                }
                Literal1.Text = htmlStr1.ToString();


                int PageCount = 0;
                StringBuilder htmlStr2 = new StringBuilder();
                IList<Models.UserInfo> list2 = Models.UserInfo.FindInPage(2, 3, ref PageCount);
                foreach (Models.UserInfo item in list2)
                {
                    htmlStr2.Append("编号:" + item.ID + "  名称:" + item.Name + "<br />");
                    htmlStr2.Append("&nbsp;&nbsp;&nbsp;&nbsp;我的主题:<br />");
                    foreach (Models.ThemeInfo item2 in item.ThemeInfos)
                    {
                        htmlStr2.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编号:" + item2.ID + "  名称:" + item2.Subject + "<br />");
                    }
                }
                Literal2.Text = htmlStr2.ToString();
                Literal3.Text = PageCount + "";

            }

 

分页查询:

//分页获取数据
        public static IList<UserInfo> FindInPage(int PageIndex, int PageSize, ref int PageCount)
        {
            int BeginIndex = ((PageIndex - 1) * PageSize);
            string hql = " from UserInfo c ";
            SimpleQuery<UserInfo> query = new SimpleQuery<UserInfo>(hql);
            query.SetQueryRange(BeginIndex, PageSize);//设置从哪一条开始

            ScalarQuery squery = new ScalarQuery(typeof(UserInfo), "select Count(*) from UserInfo");
            int recordCount = Convert.ToInt32(ExecuteQuery(squery));//获得总条数
            PageCount = (recordCount / PageSize) + (recordCount % PageSize > 0 ? 1 : 0);

            return query.Execute();
        }

  

二、使用Where子句

[HasMany(typeof(ThemeInfo), Table = "ThemeInfo", ColumnKey = "UserID", Lazy = true, Cascade = ManyRelationCascadeEnum.Delete,Where="UserID=1")]
        public virtual IList<ThemeInfo> AdultThemeInfos { get; set; }

  查询所有UserID等于1的用户 发布的主题信息。Where中的条件语句须是表【Table="ThemeInfo"】中的字段名,不是属性名称。

 

以上是关于Castle ActiveRecord学习的主要内容,如果未能解决你的问题,请参考以下文章

Castle ActiveRecord学习笔记三:初始化配置

想在 C# 中学习 ActiveRecord 模式 [关闭]

Castle.ActiveRecord多数据库配置

Castle.ActiveRecord环境配置

Castle ActiveRecord + NHibernate 中的交易傻瓜

在 Castle ActiveRecord 中按 ID 选择