1.数据表

  Category:类别标签表(字段Type=1为类别,Type=2为标签)

  Category_Post:类别标签与文章中间表

  Post:文章表

  Comment:评论表

  2.数据库关系图

  Castle ActiveRecord框架学习(二):快速搭建简单博客网站

  3.简单说明

  Category和Post表为多对多关系

  Post和Comment表 为一对多关系

二、实体类

  1.Category类:

  // 指定数据表,Lazy为延迟加载
    [ActiveRecord("Category",Lazy=true)]
    public class Category : ActiveRecordBase<Category>
    {
        // 指定数据表中的主键
        [PrimaryKey("Id")]
        public virtual int Id { get; set; }

        // 指定数据表中的列
        [Property("Title")]
        public virtual string Title { get; set; }

        [Property("Description")]
        public virtual string Description { get; set; }

        [Property("DateAdded")]
        public virtual DateTime DateAdded { get; set; }

        [Property("Type")]
        public virtual int Type { get; set; }

        // 多对多
        // typeof(Post):对方表的实体类,Table:关联中间表,ColumnRef:关联中间表中与对方实体相关的列,ColumnKey:关联中间表中与本实体相关的列,Lazy:延迟加载,通过本实体获取对方实体信息时,才会去数据库查询
        [HasAndBelongsToMany(typeof(Post),Table = "Category_Post", ColumnRef = "Post_Id", ColumnKey = "Category_Id",Lazy=true)]
        public virtual IList<Post> Posts { get; set; }


        public static IList<Category> FindAllForTopCategory()
        {
            SimpleQuery<Category> query = new SimpleQuery<Category>(@" from Category c where c.Type=1");
            return query.Execute();
        }

        public static Category Find(int id)
        {
            return FindByPrimaryKey(id);
        }
    }
View Code

相关文章:

  • 2022-12-23
  • 2021-05-23
  • 2021-06-08
  • 2021-08-08
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2021-05-10
  • 2021-04-24
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2021-04-09
相关资源
相似解决方案