dbentry是一个开源的ORM(关系对象模型)框架,http://dbentry.codeplex.com/这是在托管网站上的项目,更详细的资料可在这里查看。写这篇文章的目的是在学习了新东西后及时的记录下来,以加深自己的理解。
- 下载安装
从托管网站下载下来的安装包为DbEntry.Net.v4.1.Setup.zip,这是最新版本。安装完成之后,会在vs里创建一个模板,用于日后项目中创建实体类库。
2. 新建实体类库项目
VS右键解决方案,添加-新建项目,从已安装的模板中选择DbEntryClassLibrary模板。这个模板就是安装dbentry之后的创建的,如果不使用这个模板来新建实体类库项目,在运行项目时会报错。当时被这问题困扰了好久,在网上找了很多才看到的。下面的就是我设计的实体类:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Lephone.Data.Definition; 6 7 namespace CmsEntryModel.Models 8 { 9 #region 用户实体 10 /// <summary> 11 /// 用户 12 /// </summary> 13 public class TbUser : DbObjectModel<TbUser> 14 { 15 16 /// <summary> 17 ///用户名 18 /// </summary> 19 public string UserName 20 { 21 get; 22 set; 23 } 24 25 /// <summary> 26 ///密码 27 /// </summary> 28 public string UserPWD 29 { 30 get; 31 set; 32 } 33 34 /// <summary> 35 ///权限 0为普通用户,1为管理员 36 /// </summary> 37 public int Authority 38 { 39 get; 40 set; 41 } 42 43 /// <summary> 44 ///邮箱 45 /// </summary> 46 public string Email 47 { 48 get; 49 set; 50 } 51 52 /// <summary> 53 ///创建日期 54 /// </summary> 55 public DateTime CreateDate 56 { 57 get; 58 set; 59 } 60 } 61 #endregion 62 63 64 65 #region 文章实体 66 /// <summary> 67 /// 文章 68 /// </summary> 69 public class Article : IDbObject 70 { 71 72 /// <summary> 73 ///文章ID 74 /// </summary> 75 [DbKey] 76 public int ArticleId 77 { 78 get; 79 set; 80 } 81 82 /// <summary> 83 ///文章标题 84 /// </summary> 85 public string Title 86 { 87 get; 88 set; 89 } 90 91 /// <summary> 92 ///内容 93 /// </summary> 94 public string Content 95 { 96 get; 97 set; 98 } 99 100 /// <summary> 101 ///文章介绍图片 102 /// </summary> 103 public string ArticleImage 104 { 105 get; 106 set; 107 } 108 109 /// <summary> 110 ///作者 111 /// </summary> 112 public string Author 113 { 114 get; 115 set; 116 } 117 118 /// <summary> 119 ///排序 120 /// </summary> 121 public int Sort 122 { 123 get; 124 set; 125 } 126 127 /// <summary> 128 ///文章分类ID 129 /// </summary> 130 public int ArticleCategoryId 131 { 132 get; 133 set; 134 } 135 136 /// <summary> 137 ///点击量 138 /// </summary> 139 public int ClickRate 140 { 141 get; 142 set; 143 } 144 145 /// <summary> 146 ///发布日期 147 /// </summary> 148 public DateTime CreateDate 149 { 150 get; 151 set; 152 } 153 } 154 155 #endregion 156 } 157 }
需要引用这个命名空间Lephone.Data.Definition。
在设计实体类的时候,需要实现IDbObject接口,并且主键属性必须用[Dbkey]特性声明。或者继承DbObjectModel<T>类,此时设计的实体类不能包括Id属性,因为在
DbObjectModel<T>类定义了了Id属性,并且把Id标记为了主键字段。我在这个类的定义中可以看到:
View Code
using System; namespace Lephone.Data.Definition { [Serializable] public class DbObjectModel<T, TKey> : DbObjectModelBase<T, TKey> where T : global::Lephone.Data.Definition.DbObjectModel<T, TKey>, new() where TKey : struct { public DbObjectModel(); [DbKey] public TKey Id { get; set; } } }
3. 配置web.config
web.config的配置方式如下,需要注意的是下面展示的配置信息必须位于<configuration>节点下的最前面,即<appSettings>,<connectionStrings>等这些节点必须在下面配置信息节点的后面,否则会报错。
View Code
<configSections> <section name="Lephone.Settings" type="Lephone.Core.Setting.NameValueSectionHandler, Lephone.Core"/> </configSections> <Lephone.Settings> <add key="AutoCreateTable" value="true"/> <add key="ObjectHandlerType" value="Reflection" /> <add key="DataBase" value="@Access : @~App_Data\cms.mdb"/> </Lephone.Settings>