C#,Castle,NHibernate,Oracle,最简单的实现

第一步(这一步骤也可以省略的):创建表

-- Create table
create table USERS
(
  logonid      NVARCHAR2(255) not null,
  logonname    NVARCHAR2(255),
  password     NVARCHAR2(255),
  emailaddress NVARCHAR2(255),
  lastlogon    NUMBER(10)
)
tablespace USER_DATA
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints
alter table USERS
  add primary key (LOGONID)
  using index
  tablespace USER_DATA
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

 

2.编辑,类文件

添加引用

using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

castle的引用在www.castleproject.org上有很多,下载一个包里,把里面的dll,引用进来就可以了

C#,Castle,NHibernate,Oracle,最简单的实现_第1张图片

由于是Oracle,所以少不了对Oracle的dll

[ActiveRecord("USERS")]
    public class User : ActiveRecordBase
    {
        private string _id;

        private string _name;

        private string _password;

        private string _emailAddress;

        private int _lastLogon;

        [PrimaryKey(PrimaryKeyType.UuidHex, Params = "format=D,seperator=-"
, Column = "LOGONID")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property("LOGONNAME")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property("PASSWORD")]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        [Property("EMAILADDRESS", Length = 3)]
        public string Address
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }

        [Property("LASTLOGON")]
        public int LastLogon
        {
            get { return _lastLogon; }
            set { _lastLogon = value; }
        }

        public static void DeleteAll()
        {
            ActiveRecordBase.DeleteAll(typeof(User));
        }

        public static User[] FindAll()
        {
            return ((User[])(ActiveRecordBase.FindAll(typeof(User))));
        }

        public static void Save(User user)
        {
            ActiveRecordBase.Save(user);
        }

 

注意粗体显示的Attribute,这样可以生成GUID,这里是为了解决DbType.Guid的不支持而进行的改变

 

3.然后配置app.Config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
  </configSections>
  <activerecord>
    <config>
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver"/>
      <add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect"/>
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
      <add key="hibernate.connection.connection_string" value="Data Source=orcl;User ID=trade;Password=trade;"/>
    </config>
  </activerecord>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

4.添加完了之后 在Program.cs里面进行 Initialize

 IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;

            ActiveRecordStarter.Initialize(source,typeof(User));

放在Main函数中即可。

5.调用

比如下面的调用:

  private void button2_Click(object sender, EventArgs e)
        {
            User usexx = new User();
            usexx.Name = "1";
            usexx.Address = "3333wertwertwertwert";
            usexx.LastLogon =1;
            usexx.Save();
        }

这样就顺利在Oracle,的USERS的表中就添加了一条数据。

这就是一个最简单的Castle应用了。

注意:

一定要保证你的引用是同一个版本的

using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

还有

当出现Cannot Save的时候大部分的问题是你的数据的格式不对。

现在发现使用 :

Castle.ActiveRecord.Generator.exe自动生成类对象,是有异常的,所以还是敲代码吧。如果哪位知道什么原因,请指点呀。

多分享,多受益。我为人人,人人为我。赠人玫瑰,手留余香。

点击链接刷刷流量吧:http://shop70757995.taobao.com/

你可能感兴趣的:(C#,Castle,NHibernate,Oracle,最简单的实现)