1、下载Castle.ActiveRecord-3.0.RC,新建项目,将下载到的包中所有dll引入到项目中。
2、连接数据库
1)配置文件的方式:
在EXE输出目录中新建一个appconfig.xml:
<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<config>
<add
key="connection.driver_class"
value="NHibernate.Driver.SqlClientDriver" />
<add
key="dialect"
value="NHibernate.Dialect.MsSql2008Dialect" />
<add
key="connection.provider"
value="NHibernate.Connection.DriverConnectionProvider" />
<add
key="connection.connection_string"
value="Data Source=.;Initial Catalog=XXX;Persist Security Info=True;User ID=sa;Password=******" />
<add key="proxyfactory.factory_class"
value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
</config>
</activerecord>
程序启动时执行:
XmlConfigurationSource source = new XmlConfigurationSource("AppConfig.xml");
ActiveRecordStarter.Initialize(source, typeof(User), typeof(Blog), typeof(Post));
2)不用配置文件,直接使用代码连接数据库:
在程序启动时执行:
InPlaceConfigurationSource source = new InPlaceConfigurationSource();
IDictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2008Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
properties.Add("connection.connection_string", "UID=sa;Password=123qwe!@#;Initial Catalog=IMS;Data Source=.;");
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(source, typeof(User));
数据库连接成功。
3、定义数据库表,比如创建User表:
CREATE TABLE [dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NOT NULL,
[Password] [varchar](50) NOT NULL,
CONSTRAINT [PK_USER] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
4、定义表对应的类,并添加属性注释:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
namespace ActiveRecordTest.IMS.Model
{
[ActiveRecord("[User]")]
public class User : ActiveRecordBase
{
private int id;
private string userName;
private string password;
[PrimaryKey(PrimaryKeyType.Identity)]
public int Id
{
get { return id; }
set { id = value; }
}
[Property]
public string UserName
{
get { return userName; }
set { userName = value; }
}
[Property]
public string Password
{
get { return password; }
set { password = value; }
}
public static int GetUserCount()
{
return FindAll(typeof(User)).Length;
}
}
}
5、测试代码
var cnt = User.GetUserCount();
Console.WriteLine(cnt);
for (int i = 0; i < 100; i++)
{
User u = new User { UserName = i.ToString(), Password = "" };
u.Create();
}
总结:ActiveRecord也是采用NHibernate框架,但简化了配置,使用很方便。并可以方便的实现主从表关系的配置,具体配置方法可从网上搜索。
事务支持:
Blog blog = new Blog();
blog.BlogName = "Henreash Blogs";
blog.BlogAuthor = "Henreash";
using (TransactionScope tran = new TransactionScope())
{
try
{
blog.Create();
for (int i = 0; i < 10; i++)
{
Post post = new Post();
post.PostTitle = "This is my " + i.ToString() + " post";
post.PostCategories = "Castle";
post.Blog = blog;
post.PostCreate = DateTime.Now;
post.Save();
}
tran.VoteCommit();
}
catch
{
tran.VoteRollBack();
}
}