Castle ActiveRecord学习实践(4)关系映射

前面第2篇文章说了下基础的映射,本篇说下实体的关系映射

先来看看 One-To-One关系

MERJ(RS]ADCX2MP$6L{D2TN

准备数据库表

UBXADR6G0LIYWF[1}Q}%HTD

product表

A}YTETF{%~D`C8L(QUIJI6B

detail表 

需要注意:

  1. 副表 detail 的主键不能为自增类型
  2. 主表Product与detail表的主键名必须一致

编写实体类

product.cs

   1:  [ActiveRecord("Product")]
   2:  public class Product:ActiveRecordBase<Product>
   3:  {
   4:      private Detail _detail;
   5:      [PrimaryKey]
   6:      public int Id { get; set; }
   7:   
   8:      [Property]
   9:      public Decimal Price { get; set; }
  10:   
  11:      [Property]
  12:      public string Name { get; set; }
  13:   
  14:      [OneToOne(Cascade=CascadeEnum.All)]
  15:      public Detail Detail {
  16:          get {
  17:              if (_detail==null)
  18:              {
  19:                  _detail = new Detail();
  20:              }
  21:              return _detail;
  22:          }
  23:          set {
  24:   
  25:              _detail = value;
  26:          }
  27:      }
  28:  }
  29:   

 

 

detail.cs

   1:  [ActiveRecord("Detail")]
   2:  public class Detail:ActiveRecordBase<Detail>
   3:  {
   4:      private Product _product;
   5:     
   6:      [PrimaryKey(PrimaryKeyType.Foreign)]
   7:      public int Id { get; set; }
   8:   
   9:      [Property]
  10:      public string Description { get; set; }
  11:   
  12:      [OneToOne]      
  13:      public Product Product {
  14:          get {
  15:              if (_product==null)
  16:              {
  17:                  _product = new Product();
  18:              }
  19:              return _product;
  20:          }
  21:          set {
  22:              _product = value;
  23:          }
  24:      }
  25:  }

 

 

表示层调用代码

   1:  using (TransactionScope tran=new TransactionScope ())
   2:  {
   3:      Product product = new Product();
   4:      product.Name = "商品名称";
   5:      product.Price = 6.39m;
   6:      Detail detail = new Detail();
   7:      detail.Description = "商品详情";
   8:      product.Detail = detail;
   9:      detail.Product = product;
  10:      product.Save();
  11:      tran.VoteCommit();
  12:  }

 

 

One-To-One 属性说明

属性

类型

说明

CascadeCascadeEnumFrom NHibernate docs: specifies which operations should be cascaded from the parent object to the associated object.
指明哪些操作会从父对象级联到关联对象
Constrained boolFrom NHibernate docs: specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which Save() and Delete() are cascaded (and is also used by the schema export tool).

指定关联的类映射表引用的表的主键,外键约束。 此选项会影响在其中保存()和删除()在级联(也可用于在使用schema导出工具)。

Fetch FetchEnumFrom NHibernate docs: Chooses between outer-join fetching or sequential select fetching.
选择在外连接抓取或者序列选择抓取选择其一
ForeignKey string 
MapType TypeAllows one to reference a different type than the property type
允许一个引用类型不同的属性类型
PropertyRef stringFrom NHibernate docs: The name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used.
加入到这个类的主键关联的类的属性的名称。 如果没有指定,使用的主键关联的类。

One-To-Many

继续使用第一篇文章的例子

post.cs

   1:  [ActiveRecord("Posts")]
   2:  public class Post : ActiveRecordBase<Post>
   3:  {
   4:      [PrimaryKey("PostId")]
   5:      public int Id { get; set; }
   6:   
   7:      [Property]
   8:      public string Subject { get; set; }
   9:   
  10:      [Property]
  11:      public string Text { get; set; }
  12:   
  13:      [Property]
  14:      public DateTime DateAdded { get; set; }
  15:   
  16:      [HasMany]
  17:      public IList<Comment> Comments { get; set; }
  18:  }

 

 

comment.cs

   1:  [ActiveRecord("Comments")]
   2:  public class Comment:ActiveRecordBase<Comment>
   3:  {
   4:      [PrimaryKey("CommentId")]
   5:      public int Id { get; set; }
   6:   
   7:      [Property]
   8:      public string Text { get; set; }
   9:   
  10:      [Property]
  11:      public string Author { get; set; }
  12:   
  13:      [Property]
  14:      public DateTime DateAdded { get; set; }
  15:   
  16:      [BelongsTo("PostId")]
  17:      public Post Post { get; set; }
  18:   
  19:  }

 

 

One-To-Many属性说明

HasManyAttribute

属性

类型

说明

CascadeManyRelationCascadeEnum指明哪些操作会从父对象级联到关联的对象,如果不指定,则为None
Cascade=ManyRelationCascadeEnum.All
ColumnKeystring指定关联类的一个属性,这个属性将会和本外键相对应。
Inversebool指定是否级联操作
Lazybool

指定是否延迟加载关联对象

Schemastring指定Schema的名字
Tablestring指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略
Wherestring指定一个附加SQL的Where子句

注意 HasManyAttribute继承自RelationAttribute 还有许多属性为列出

BelongsToAttribute 

属性

类型

说明

CascadeManyRelationCascadeEnum指明哪些操作会从父对象级联到关联的对象,如果不指定,则为None
Cascade=ManyRelationCascadeEnum.All
Columnstring列名,外键字段名
Insertbool是否允许增加
NotNullbool是否允许为空
OuterJoinOuterJoinEnum是否允许外连接抓取
Uniquebool是否唯一
Updatebool是否允许更新

Many-To-Many

demo 使用Castle ActiveRecord学习实践(2) 映射中的 post tag 例子

转载于:https://www.cnblogs.com/whx1973/archive/2012/10/15/2723698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值