Castle ActiveRecord实现one–one关系的映射

一.编写实体类代码为了实现多对多的关系,我们要在Customer 、CustomerAddress

类中分别使用OneToOne特性。示例代码:


Customer 类:

  1. [ActiveRecord("Customer")]
  2.     public class Customer : ActiveRecordBase<Customer>
  3.     {
  4.         private Guid custID;
  5.         private string name;
  6.         private CustomerAddress addr;
  7.         [PrimaryKey]
  8.         public Guid CustomerID
  9.         {
  10.             get { return custID; }
  11.             set { custID = value; }
  12.         }
  13.         [OneToOne(Cascade=CascadeEnum.All)]
  14.         public CustomerAddress CustomerAddress
  15.         {
  16.             get { return addr; }
  17.             set { addr = value; }
  18.         }
  19.         [Property]
  20.         public string Name
  21.         {
  22.             get { return name; }
  23.             set { name = value; }
  24.         }
  25.     }
复制代码

CustomerAddress 类:

  1. [ActiveRecord("CustomerAddress")]
  2.     public class CustomerAddress : ActiveRecordBase<CustomerAddress>
  3.     {
  4.         private Guid custID;
  5.         private string address;
  6.         private Customer cust;
  7.         [PrimaryKey(PrimaryKeyType.Foreign)]
  8.         public Guid CustomerID
  9.         {
  10.             get { return custID; }
  11.             set { custID = value; }
  12.         }
  13.         [OneToOne]
  14.         public Customer Customer
  15.         {
  16.             get { return cust; }
  17.             set { cust = value; }
  18.         }
  19.         [Property]
  20.         public string Address
  21.         {
  22.             get { return address; }
  23.             set { address = value; }
  24.         }
  25.     }
复制代码

二.初始化数据库
根据实体类来逆向的创建数据库的表(数据库要是已存在的)

  1. IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
  2.             ActiveRecordStarter.Initialize(source, typeof(Customer), typeof(CustomerAddress));
  3.             ActiveRecordStarter.CreateSchema();//初始化数据库
复制代码

三.编写测试代码
1.级联增加

  1. /// <summary>
  2.         /// 添加数据
  3.         /// </summary>
  4.         private static void AddSouces()
  5.         {
  6.             //使用事务处理
  7.             using (TransactionScope tran = new TransactionScope())
  8.             {
  9.                 try
  10.                 {
  11.                     Customer cust = new Customer();
  12.                     cust.CustomerID = new Guid();
  13.                     cust.Name = "张三";
  14.                     CustomerAddress custAddress = new CustomerAddress();
  15.                     custAddress.Address = "address";
  16.                     //支出对应关系
  17.                     cust.CustomerAddress = custAddress;
  18.                     custAddress.Customer = cust;
  19.                     custAddress.Save(); //保存Customer对象
  20.                     tran.VoteCommit();  //执行事务
  21.                 }
  22.                 catch (Exception)
  23.                 {
  24.                     tran.VoteRollBack();    //若出现异常,回滚事务
  25.                     
  26.                 }
  27.             }
  28.         }
复制代码

2级联删除:因为我们有设置Cascade=CascadeEnum.All,删除一个Customer,对应的CustomerAddress 记录也会删除

  1. SimpleQuery<Customer> query = new SimpleQuery<Customer>(typeof(Customer), "From Customer as n where n.Name = ?", "李四");
  2.             Customer cust = query.Execute().FirstOrDefault();
  3.             cust.Delete();//级联删除,CustomerAddress表的相应数据也会删除
复制代码

posted on 2012-11-22 09:39  一个石头  阅读(146)  评论(0)    收藏  举报