1.数据表
Category:类别标签表(字段Type=1为类别,Type=2为标签)
Category_Post:类别标签与文章中间表
Post:文章表
Comment:评论表
2.数据库关系图
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); } }