• 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 属性说明

    属性

    类型

    说明

    Cascade CascadeEnum From NHibernate docs: specifies which operations should be cascaded from the parent object to the associated object.
    指明哪些操作会从父对象级联到关联对象
    Constrained bool From 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 FetchEnum From NHibernate docs: Chooses between outer-join fetching or sequential select fetching.
    选择在外连接抓取或者序列选择抓取选择其一
    ForeignKey string  
    MapType Type Allows one to reference a different type than the property type
    允许一个引用类型不同的属性类型
    PropertyRef string From 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

    属性

    类型

    说明

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

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

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

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

    BelongsToAttribute 

    属性

    类型

    说明

    Cascade ManyRelationCascadeEnum 指明哪些操作会从父对象级联到关联的对象,如果不指定,则为None
    Cascade=ManyRelationCascadeEnum.All
    Column string 列名,外键字段名
    Insert bool 是否允许增加
    NotNull bool 是否允许为空
    OuterJoin OuterJoinEnum 是否允许外连接抓取
    Unique bool 是否唯一
    Update bool 是否允许更新

    Many-To-Many

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

  • 相关阅读:
    第一课:数据库介绍篇
    爬虫day03 cast
    python excel
    爬虫 cast_day05_多进程_多线程_协程_selenium的使用
    进程_线程_协程回顾!
    进程_线程_协程回顾 2!
    day 06 爬虫
    看正月点灯笼老师的笔记 —动态规划2.1
    动态规划——放苹果

  • 原文地址:https://www.cnblogs.com/whx1973/p/2723698.html
Copyright © 2020-2023  润新知