说明:MiniAccess原来需要微软企业库中数据访问ApplicationBlock的支持。后来发现DAAB应用程序块只是一个System.Data的很轻很薄的包装,因而直接把相关的源代码直接嵌入到MiniAccess中。
在源代码文件中的Demo和DemoSite演示了MiniAccess的基本使用。使用MiniAccess的要点包括如下几点:
上述步骤中完成1、2两步就可以编译生成数据访问的程序集了。3、4两步的作用是配置数据访问程序集在运行时的环境信息,在使用Codesmith生成数据访问代码文件时,已经以注释的形式生成了默认的OR映射文件的内容,可以直接复制到mdm.xml中文件中即可。
下面以Demo为例,说明基本的操作过程。
1: <?xml version="1.0"?>
2: <codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
3: <propertySets>
4: <propertySet output="DemoAccess.cs" template="..\..\CodeSmithTemplate\MiniAccess.cst">
5: <property name="DeveloperName">Coolman</property>
6: <property name="Database">
7: <connectionString>Data Source=.\sqlexpress;Initial Catalog=DemoDatabase;Integrated Security=True;Pooling=False</connectionString>
8: <providerType>SchemaExplorer.SqlSchemaProvider,SchemaExplorer.SqlSchemaProvider </providerType>
9: </property>
10: <property name="Namespace">Demo.EntityAccess</property>
11: <property name="Provider"></property>
12: <property name="AccessClass"></property>
13: <property name="Tables"></property>
14: </propertySet>
15: </propertySets>
16: </codeSmith>
在该文件中,propertyset中的output指定要生成的源代码文件,template指定MiniAccess.cst所在位置,可以使用相对路径。
DeveloperName:指定开发者的名字。
Database:指定要处理的数据库,需要进一步指出数据库的连接字符串和CodeSmith中用来分析该数据库的架构提供者。
Namespace:生成的实体的命名空间,缺省是<数据库名字>.EntityAccess。
Provider:Ado.Net中的数据提供者,目前支持SqlServer,数据库,计划中要支持的数据库还有Sqlite。
AccessClass:存取数据库的类型名称。通过该类型中提供的方法可以实现数据库中所有表的数据存取。
Tables:指出要生成数据实体类型的表的名字,不指定该属性表示为数据库中所有用户表都生成实体类型,若要指定多个表名,请使用逗号分隔。
1: <configuration>
2: <configSections>
3: <section name="miniAccess" type="Ruandao.MiniAccess.MiniAccessSection,Ruandao.MiniAccess" />
4: </configSections>
5:
6: <miniAccess>
7: <dataProviders>
8: <add name="DemoDatabase" accessType="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DemoDatabase;Integrated Security=True" />
9: </dataProviders>
10: </miniAccess>
11:
12: <system.web>
13: <compilation debug="false" targetFramework="4.0" />
14: </system.web>
15:
16: </configuration>
1: <%@ Page Language="C#" %>
2: <%@ Import Namespace="Demo.EntityAccess" %>
3:
4: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5:
6: <script runat="server">
7:
8: </script>
9:
10: <html xmlns="http://www.w3.org/1999/xhtml">
11: <head runat="server">
12: <title></title>
13: </head>
14: <body>
15: <form id="form1" runat="server">
16: <div>
17: <table border="1" cellpadding="5" style="border-collapse:collapse;" >
18: <tr>
19: <th>产品编号</th>
20: <th>分类</th>
21: <th>标题</th>
22: <th>价格</th>
23: <th>说明</th>
24: </tr>
25: <% foreach (ProductEntity entity in ProductEntity.GetAllEntities())
26: { %>
27: <tr>
28: <td><%= entity.ProductId %></td>
29: <td><%= entity.GetCatelog().Title %></td>
30: <td><%= entity.Title %></td>
31: <td><%= entity.Price.ToString() %></td>
32: <td><%= entity.Description %></td>
33: </tr>
34: <%} %>
35: </table>
36: </div>
37: </form>
38: </body>
39: </html>
页面显示结果如下: